0

I m trying to query two custom post types for data and the below isn't working. I tried a few versions ... but no luck.

  $args = array(
    'post_type' => array('custom1', 'custom2'),
      'meta_query' => array(
      'relation' => 'AND',
      //the first two keys are custom fields found in custom1 custom posts
      array(
        'key' => 'firstKey',
        'value' => 3
      ),
      array(
        'key' => 'secondKey',
        'value' => cool
      ),
      //the third key can be found in custom2 custom posts
      array(
        'key' => 'thirdKey',
        'value' => 3
      )
      )
  );
9
  • What posts are you trying to get, and what's not working?
    – user488187
    Feb 22, 2015 at 22:52
  • Trying to get custom fields from the post where thirdKey = 3. Now the custom fields are blank.
    – Ciprian
    Feb 22, 2015 at 22:55
  • Your AND relation means that only posts that have ALL of the keys specified will be returned. From your comments, it sounds like that will never happen.
    – user488187
    Feb 22, 2015 at 23:03
  • so how should I format this? another query?
    – Ciprian
    Feb 22, 2015 at 23:05
  • Yes, a different query, but your answer to the first comment is a little confusing. If you are just looking for a post with thirdkey = 3 and thirdkey can only be in post type custom2, why don't you just use a simple query with those values?
    – user488187
    Feb 22, 2015 at 23:12

1 Answer 1

0

Based on your comments: If you want to use custom queries for different types of users, then you probably want to create your query dynamically.

For example, if users of type 'cool' get special treatment, then the basic idea would be:

$args = array( 'post_type' => 'custom' );
if (user is 'cool') {
    $args['meta_query'] = array(
         'relation' => 'AND',
         array(
             'key' => 'secondKey',
             'value' => 'cool'
         ),
         array(
             'key' => 'thirdKey',
             'value' => 3
         )
    };
} else {
    $args['meta-key'] = 'thirdKey';
    $args['meta_value'] = 3
}

This just pass this as WP_Query($args) like you would if it had been static.

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.