0

I'm trying to query 'live' posts for 'venue' field existing (venue field is ID's of venue pages). Query those venue IDs/posts for which ones have a field 'country' that's value (ID) matches a specific country value.

$shows = get_posts(array(
    'post_type' => 'live',
    'meta_query' => array(
        array(
            'meta_key' => 'venue',
                'meta_query' => array(
                    array(
                        'meta_key' => 'country',
                        'value' => 'country_1',
                    )
                )
            )
    )

));

50dollanote
  • 189
  • 4
  • 4
  • 12

1 Answers1

0

WP_Query isn't designed to make such query-in-query logic. It should have one single post_type, one single meta_query.

That's why you need to use 2 queries instead.

$live_posts = new WP_Query( array (
     'post_type'=> 'live'
     'posts_per_page'        => -1,
         'meta_key'=>'venue_key_name',
         'meta_value'=>'venue_key_value',
     'fields' => 'ids'
 ));

$venue_posts=get_posts(array(
    'post_type'=>'venue',
    'meta_key'=>'country_meta_name',
    'meta_value'=> $live_posts->posts,
    'meta_compare'=>'IN'
));

In the given code you need to change meta key and values of course.

Update:

If your input parameter is array of venue IDs and your output is supposed to be country posts of those venues, then you can use these queries below:

global $wpdb;

$countries=$wpdb->get_col("SELECT meta_value FROM $wpdb->postmeta 
WHERE meta_key='country' AND post_id IN (".implode(",",$array_of_venue_page_ids).")");

if(empty($countries))$countries=[0];

$live_posts = new WP_Query( array (
     'post_type'=> 'live'
     'post__in' => $countries,
 ));
Elvin Haci
  • 3,503
  • 1
  • 17
  • 22
  • Hi Elvin, thanks for the response. I'm struggling to make it work on the code above to find what I need to do, I've tried to explain what Im looking to do as my code as it's incorrect may be misleading and my explanation wasn't clear. I'm trying to query 'live' posts for 'venue' field existing (venue field is ID's of venue pages). Query those venue IDs/posts for which ones have a field 'country' that's value (ID) matches a specific country value whether that be a value name or a post ID etc. – 50dollanote Feb 21 '21 at 12:51
  • What is your input parameter? Venue name? Or what? – Elvin Haci Feb 21 '21 at 22:28
  • It's all Post ID's for every field throughout – 50dollanote Feb 22 '21 at 00:02