-1

I have a CPT locations with a custom taxonomy regions using hierarchical terms (State is the parent term, City is the child term.

Regions Custom Taxonomy

  • North Carolina (parent)
    • Charlotte (child)
    • Raleigh (child)
  • Georgia (parent)
    • Atlanta (child)

Using an ACF Relationship field, I'm trying to build a flexible content panel that allows users to select locations and have them displayed grouped via the parent term, and then the child term like such:

Display/Result

  • North Carolina
    • Charlotte
      • Wynnchester Road Location (post)
      • Tryon Road Location (post)
    • Raleigh
      • Boylan Street Location (post)
  • Georgia
    • Atlanta
      • Wynnchester Road Location (post)

Anyone ever attempted something like this? I'm having a heck of a time and could use some help.

closed as off-topic by Tim Malone, Jacob Peattie, Nathan Johnson, Krzysiek Dróżdż, Christine Cooper Apr 15 '18 at 20:53

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third party plugins and themes are off topic, they are better asked about at their developers' support routes." – Tim Malone, Jacob Peattie, Nathan Johnson, Krzysiek Dróżdż, Christine Cooper
If this question can be reworded to fit the rules in the help center, please edit the question.

0

Why do you need a relationship field? Can't you directly use the regions taxonomy with your CPT for the linking to happen?

Now, the filtering of posts by regions can be done easily using a filtering plugin like FacetWP (paid), Search & Filter (free). In order to group the results, plugins like FacetWP have a sort-by feature which can be tweaked to group by your taxonomy. Otherwise without a plugin, it should be a complicated and lengthy code.

  • I'm not actually trying to 'filter' the locations but group them together based on state & city. I know what FacetWP is and it's functionality is not what i'm looking for in this situation. – Laurel Jan 24 '18 at 19:22
  • Then perhaps, query for all regions, and foreach region, again query for posts associated, and display them. That's a WP Taxonomy Query > Foreach Term/Region > WP Post Query. – aswinshenoy Jan 28 '18 at 7:29
0

Figured it out after a decent chunk of time and several queries/loops etc. I'm sure this could be cleaned up or possibly written better, but in the mean time this is working for me!

<?php
// Loop through Categories and Display Posts within

// ACF Relationship Field based on CPT 'Locations' - Post ID
$locations_field = $panel['locations'];

// CPT 'Locations'
$post_type = 'locations';

// Get all the state taxonomy terms used
$state_terms = get_terms( array(
    'taxonomy' => 'states',
    'hide_empty' => false,
    'parent' => 0
) );

// Foreach term, loop through Locations posts selected in the relationship field filtered via the state terms. Not actually displaying anything with this query, but creating an array of post IDs.
foreach( $state_terms as $state_term ) : ?>
    <?php
    $args = array(
        'post_type' => $post_type,
        'posts_per_page' => -1,  //show all posts
        'post__in' => $locations_field,
        'tax_query' => array(
            array(
                'taxonomy' => 'states',
                'field' => 'slug',
                'terms' => $state_term->slug,
            )
        )

    );
    $new_posts = new WP_Query($args);
    $ids = array();
    if( $new_posts->have_posts() ): while( $new_posts->have_posts() ) : $new_posts->the_post(); ?>
        <?php array_push( $ids, get_the_ID() ); ?>
    <?php endwhile; endif; ?>

    <?php // Begin to display groups starting with the State term
      if (!empty($ids)) : // CHECK TO MAKE SURE THERE ARE POSTS! ?>
        <h2><?php echo $state_term->name; // State name ?></h2>
        <?php
        // Get all the city taxonomy terms used
        $city_terms = get_terms( array(
            'taxonomy' => 'cities',
            'hide_empty' => false,
            'parent' => 0
        ) );
        foreach( $city_terms as $city ) : ?>
            <?php
            $args2 = array(
                'post_type' => $post_type,
                'posts_per_page' => -1,  //show all posts
                'tax_query' => array(
                    array(
                        'taxonomy' => 'cities',
                        'field' => 'slug',
                        'terms' => $city->slug,
                    )
                ),
                'post__in' => $ids // The array of IDs previously gathered
            );
            $posts2 = new WP_Query($args2);
            if( $posts2->have_posts() ): ?>
                <div class="location-city">
                    <h2><?php echo $city->name; // City name ?></h2>
                    <?php while( $posts2->have_posts() ) : $posts2->the_post(); ?>
                        <h3><?php  echo get_the_title(); ?></h3>
                    <?php endwhile; ?>
                </div>
            <?php endif; ?>
        <?php endforeach; ?>
    <?php endif; ?>

<?php endforeach; ?>

Not the answer you're looking for? Browse other questions tagged or ask your own question.