1

I have a custom post type product (wine). Each of the following is a custom taxonomy:

  • Vintage (Year of harvesting) [only one per wine]
  • Family (Grape variety) [may be more than one]
  • Size (Bottle Size) [only one per product]

In the single product view of a wine I want to do the following queries:

1) Find all wines which have the same family, same size but are from a different vintage

2) Find all wines (post type: product) which are in the same family AND have the same vintage AND have a different size.

Now the problem: The numeric year is a custom term meta property of the vintage term. The numeric bottle size is a custom term meta property of the size term.

For the first query I have tried the following:

                $other_years = get_posts(array(
                  'post_type' => 'product',
                  'numberposts' => -1,
                  'tax_query' => array(
                    array(
                      'taxonomy' => 'family',
                      'field' => 'id',
                      'terms' => $family->term_id, // Where term_id of Term 1 is "1".
                      'include_children' => false
                    ),
                    array(
                      'taxonomy' => 'size',
                      'field' => 'id',
                      'terms' => $size->term_id, // Where term_id of Term 1 is "1".
                      'include_children' => false
                    ),
                    array(
                      'taxonomy' => 'vintage',
                      'field' => 'id',
                      'terms' => array($vintage->term_id), // Where term_id of Term 1 is "1".
                      'include_children' => false,
                      'operator' => 'NOT IN'
                    ),
                  )
                ));

It works. However: Instead of comparing the term_id (here: term id of vintage) I would like to compare if the term custom meta value (year_numeric) is different.

Is there any possibility to do such a query?

4

AFAIK there's no way to achieve that within a single WP_Query, so you'll have to first get a list of term_ids which have a different year than the one in question.

I think with the following you'll come quite close. (don't have a env to test right away)

$other_years_terms = get_terms(
     'taxonomy'     =>  'vintage',
     'meta_key'     =>  'year_numeric',
     'meta_value'   =>  $the_current_wine_year,   // You'll have to figure that out
     'meta_compare' =>  '!=',
     'fields'       =>  'ids'
);

$other_years_posts = get_posts(array(
              'post_type' => 'product',
              'numberposts' => -1,
              'tax_query' => array(
                array(
                  'taxonomy' => 'family',
                  'field' => 'id',
                  'terms' => $family->term_id, // Where term_id of Term 1 is "1".
                  'include_children' => false
                ),
                array(
                  'taxonomy' => 'size',
                  'field' => 'id',
                  'terms' => $size->term_id, // Where term_id of Term 1 is "1".
                  'include_children' => false
                ),
                array(
                  'taxonomy' => 'vintage',
                  'field' => 'term_id',
                  'terms' => $other_years_terms,
                ),
              )
            ));
| improve this answer | |
  • Some research was also done by me and I guess you are right. This is the only way currently. However I think they should add this functionality as it certainly exists (with posts (meta_query)). – Blackbam Jul 7 '17 at 12:02
  • Created a feature request maybe they implement it: core.trac.wordpress.org/ticket/41265 – Blackbam Jul 7 '17 at 12:54
0

I believe you are checking for when a meta key does not exist.

Please refer to this similar question: query all posts where a meta key does not exist

| 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.