0

Custom post type "Wines" has two categories: Regions & Producers. "Wines" has a value for both a Region and a Producer.

I want to loop through posts in [Category => Region, Term => X] and get each post's respective term in [Category => Producer, Term => ?].

The final goal is to go through the newly created array and:

  • Remove duplicate entry terms
  • Sort terms by alphabetical order
  • Loop through array, and list posts (wines) of each respective entry (producer term)

I know you may be asking, "Why not just go to the producer's archive?"...I know. cry The Region's archive needs to display a list of posts ordered by the terms in the producer category.

Thanks a bunch.

6
  • So just to be clear, you want to end up with a UNIQUE (no duplicates) list of Producers for 'X' region and then display each Producer's wines (which are the posts) sorted/grouped by producer?
    – aj-adl
    Commented May 19, 2014 at 23:46
  • @aj-adl: That's exactly it :)
    – user51859
    Commented May 20, 2014 at 2:37
  • Cool, should be able to come up with something, how are region and producer implemented? Have you made a custom taxonomy for each? or have you used the 'post categories' or 'post tags' systems?
    – aj-adl
    Commented May 20, 2014 at 7:04
  • @aj-adl: I used WordPress creation kit to create the custom post type "wine" and custom taxonomies of wine for region and producer.
    – user51909
    Commented May 20, 2014 at 14:08
  • @aj-adl: I used WordPress creation kit to create the custom post type "wine" and custom taxonomies of wine for region and producer.
    – user51859
    Commented May 20, 2014 at 14:20

1 Answer 1

0

So based in the information you've given me in the comments the following gist should work as you have described with your current setup.

https://gist.github.com/aj-adl/fab2cefd1450cbf24a5c

You provide it with a region, and it will get all posts from a region, loop through and build an array of producers, then do a subquery and loop for each producer.

It's definitely not the most efficient way of doing it, and the main reason why we have to stuff around with everything is because WordPress doesn't allow you to group by taxonomies.

If you changed the producers to being a meta value of a post, instead of a taxonomy, then you could do pretty much the same thing in this simple query..

$args = array(
    'post_type'  => 'wine',
    'orderby'    => 'meta_value menu_order',
    'meta_query' => array(
            array(
                'key'     => 'producer',
                'compare' => 'EXISTS',
            )
    ),
    'tax_query'   => array(
            array(
                'taxonomy' => 'region',
                'field'    => 'slug',
                'terms'    => $region_slug // your term here (the region)
            )
        ),
);
$wine_query = new WP_Query( $args );

// Then do your loop 

It would be pretty easy to still insert heading sections whenever the producer value changes (just check for it as part of the loop)

I used the same plugin you did to setup the custom fields - my post_type was 'wine', and taxonomies where 'producer' and 'region'.

1
  • You are the most man of men!
    – user51859
    Commented May 22, 2014 at 13:24

Your Answer

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

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