1

Sorry for confusing title. Not sure how to describe it better. I'd like to display all shops first sorted by theirs categories, then, inside it, sorted by their locations.

I have custom post type "Shop", two custom taxonomies "Shop Category", "Shop Location".

Example how I'd like it to display:

Shoes

London

  • Shop 01
  • Shop 02

Tokyo

  • Shop 05
  • Shop 06

Electronics

London

  • Shop 11
  • Shop 12

Tokyo

  • Shop 15
  • Shop 16

Shoes, Electronics, (...) - Taxonomy #1

London, Tokyo, (...) - Taxonomy #2

Shop 01, (...) - Custom Post Type

Got it working to show sorted by one taxonomy so far. It works good but I need one more level.

<?php
$custom_terms = get_terms('shop_category');
foreach ($custom_terms as $custom_term) {

wp_reset_query();
$args = array(
    'post_type' => 'shop',
    'tax_query' => array(
        array(
            'taxonomy' => 'shop_category',
            'field' => 'slug',
            'terms' => $custom_term->slug,
        ),
    ),
);

$loop = new WP_Query($args);
if($loop->have_posts()) {
    echo '<div class="box-category">'.$custom_term->name.'</div>';

    while($loop->have_posts()) : $loop->the_post();
        echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';
    endwhile;
}

}
?>

Thanks in advance for directions.

0

First get a list of categories and a list of locations. Then loop through each category. Within each category loop through the list of locations and query posts that have the current category and the current location and output the list:

$categories = get_terms( array(
    'taxonomy' => 'shop_category',
) );

$locations = get_terms( array(
    'taxonomy' => 'shop_location',
) );

foreach ( $categories as $category ) {
    echo '<div class="box-category">' . $category->name . '</div>';

    foreach ( $locations as $location ) {
        $shops = new WP_Query( array(
            'post_type' => 'shop',
            'tax_query' => array(
                array(
                    'terms'    => $category->term_id,
                    'taxonomy' => 'shop_category',
                ),
                array(
                    'terms'    => $location->term_id,
                    'taxonomy' => 'shop_location',
                ),
            ),
        ) );

        if( $shops->have_posts() ) {
            echo '<div class="box-location">' . $location->name . '</div>';

            while ( $shops->have_posts() ) : $shops->the_post();
                echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a><br>';
            endwhile;
        }

        wp_reset_postdata();
    }
}
  • Thank you but my taxonomies are separate, they don't relate to each other. One is "shop_category" for categories and second one is "shop_location" for cities. Is it possible to do this this way or I have to create sub categories for all "Shop Categories" with cities? – HazeHybrid Mar 8 '18 at 9:35
  • Ah, I see. I'll tweak my answer. It's won't be too different. – Jacob Peattie Mar 8 '18 at 9:39
  • @HazeHybrid I've updated my answer. – Jacob Peattie Mar 8 '18 at 9:43
  • Thank you so much! That works perfectly. This way I can only show "Tokyo" shops somewhere else in the code without much hassle. Thanks! – HazeHybrid Mar 8 '18 at 9:46

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.