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.

1 Answer 1

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();
    }
}
4
  • 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, 2018 at 9:35
  • Ah, I see. I'll tweak my answer. It's won't be too different. Mar 8, 2018 at 9:39
  • @HazeHybrid I've updated my answer. Mar 8, 2018 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, 2018 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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