1

I have created two custom taxonomies (1. Business location and 2. Business Category) for a custom post type (Business Listing). Now I have listed all the location under Location taxonomy archive where I have listed all the locations and when the user clicks on Location A it takes user to post archive page of Location A where all the posts are listed from that specific taxonomy.

Here is where I want to list each Business Category on top and below it all the post of specific Business Category

Example: in domain.com/locations/location1/

Category 1
Post 1
Post 2
Post 3

Category 2
Post 1
Post 2
Post 3

How do I accomplish this?

I did try few options but below code gets the post from all the locations but I want to list the categories and post from the current location.

            <?php
            $taxonomy = 'business-category';
            $args = array(
                'orderby' => 'id',
                'order' => 'ASC',
            );
            $taxonomy_terms = get_terms($taxonomy, $args);
            if($taxonomy_terms) {
                foreach($taxonomy_terms as $taxonomy_term) {
                    $args = array(
                        "$taxonomy" => $taxonomy_term->slug,
                        'post_status' => 'publish',
                        'posts_per_page' => -1,
                    );
                    $query = new WP_Query( $args );
                    if ( $query->have_posts() ) : ?>
                        <h2><?php echo $taxonomy_term->name; ?></h2>
                            <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                                <h3><?php the_title(); ?></h3>
                            <?php endwhile; ?>

                        <?php wp_reset_postdata(); endif;
                }
            }
            ?>

Answer Modified from Ben HartLenn

<?php
    $business_location   = get_queried_object();
    $business_categories = get_terms( [
        'taxonomy'   => 'business-category',
        'hide_empty' => true,
        ] );
    $duplicates          = [];
    foreach ( $business_categories as $business_category ) :
?>
<?php $args = [
    'post_type'    => 'business-listing',
    'tax_query'    => [
    'relation' => 'AND',
    [
    'taxonomy' => 'business-location',
    'field'    => 'slug',
    'terms'    => $business_location->slug,
    ],
    [
    'taxonomy' => 'business-category',
    'field'    => 'slug',
    'terms'    => $business_category->slug,
    ],
    ],
    'post__not_in' => $duplicates,
    ];
    $query      = new WP_Query( $args );
?>
<div class="business-listing-block">
    <?php if ( $query->have_posts() ) : ?>
        <h2 class='business-category-title'><?php echo $business_category->name; ?></h2>
        <ul class='business-listings'>
            <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                <?php if ( ! in_array( $post->ID, $duplicates ) ) {
                    $duplicates[] = $post->ID;
                } ?>
                <li class='business-listing'><?php echo the_title(); ?> </li>
            <?php endwhile; ?>
        </ul>
    <?php endif; ?>
</div>
<?php endforeach; ?>
0

Presumably that would be in your taxonomy-business-location.php template file then. Here's what the main content area could look like in such a template, basically:

<div id="main-content">
    <?php
    $business_location = get_queried_object();

    echo "<h1 class='business-location-title'>Businesses in " . $business_location->name . ":</h1><hr>"; // Location A, according to your example

    $business_categories = get_terms( [
        'taxonomy' => 'business-category',
        'hide_empty' => true, // hide empty business categories(i.e. have no posts)
    ] );

    $duplicates = []; // more on this later...

    // start looping through business categories        
    foreach ( $business_categories as $business_category ) {

        // create new query that gets business listings, that are in Location A business location, and are in the current business category from the foreach loop above
        $args = [
            'post_type' => 'business-listing',
            'tax_query' => [
                'relation' => 'AND', // business listings must have both taxonomies terms queried below
                [
                    'taxonomy' => 'business-location',
                    'field' => 'slug',
                    'terms' => $business_location->slug,
                ],
                [
                    'taxonomy' => 'business-category',
                    'field' => 'slug',
                    'terms' => $business_category->slug,
                ],
            ],
            'post__not_in' => $duplicates, // prevents duplicate business listings from showing, you may or may not want this part, more info below...
        ];
        $query = new WP_Query( $args );


        if ( $query->have_posts() ) : // don't show business category if no posts in current business category and location

            echo "<h2 class='business-category-title'>" . $business_category->name . "</h2>";

            echo "<ul class='business-listings'>";

            // looping through business listings
            while ( $query->have_posts() ) : $query->the_post();
                /* Collect business listing id's and the array gets added 
                to the next WP_Query's post__not_in. This way a business 
                listing will only display in the first business category 
                shown, and none of the other business categories that 
                the listing is in. Again, this may or may not be wanted.
                */
                if( !in_array($post->ID, $duplicates) ) {
                    $duplicates[] = $post->ID;
                }

                // output business listing
                echo "<li class='business-listing'>" . get_the_title() . "</li>";

            endwhile;

            echo "</ul>";

        endif;
    }
    ?>

</div><!-- /#main-content -->

EDIT: Here's a pic of my business listings data set up, and the output I get if it helps you out:

Business Listing Data Set Business Listing Output for BC

  • I got correct 'Businesses in location name' but did not get any categories or post list. – Kevin S Aug 9 '17 at 13:36
  • It works fine for me. You are likely going to need to update the various values to match your post type and taxonomy names exactly. Also, are you sure your data is structured properly? – Ben HartLenn Aug 9 '17 at 13:41
  • You are right. I made slight changes to accommodate some styling but it is also returning empty <div class="business-listing-block"></div> after every block. My be I messed it up. – Kevin S Aug 9 '17 at 14:38
  • I have updated my question with the modified code. Could you have a look at it? Thanks a lot. – Kevin S Aug 9 '17 at 14:45
  • What are the registered names of your two taxonomies and your custom post type? – Ben HartLenn Aug 9 '17 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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