2

I have created a CPT called 'stores', each store has a taxonomy registered to it called 'store-type' (i.e. whether it's a fashion store, or kids etc). There is a third taxonomy 'floor'. A store can be on multiple floors like a large department store.

I have been asked by the client to list all the 'stores' by 'store type' and on each floor.

So far I have an idea to create a horrible loop like: (only pseudo-code at the moment)

foreach storeLevel as level
{
   foreach storeType as type
   {
      get_posts_by_store_type() /* Note this would be a new WP_Query() */
      list_stores();
   }
}

As you can imagine, this would create hundreds of queries. There is also some custom fields on each post I need to pull through.

Could anyone point me in the direction of a better method to list posts by taxonomy?

edit: I have tested a similar bit of code on just listing the stores without going through each level and it's created around 80 queries and only a 5th of the stores have been added. :/

1

Without spending too long thinking about this one I would imagine you could do it with one query.

$store_array = array();

while (have_posts()) : the_post();
    $store_type = // get term id, slug, name from store type taxonomy
    $store_floor = // get term id, slug, name from floor taxonomy
    $store_array[$store_type][$store_floor] = $post->ID;
endwhile;

// run a foreach on your $store_array

From there you could sequentially list your store types and sub-group by floors within it. Post ID will give you option to get_the_title() and get_permalink().

If you needed more detail you could add another dimension to the array.

| improve this answer | |

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.