0

I am having two custom post type "meet_our_community" and "news"

I have added a checkbox field named 'add_to_meet_our_community' from the acf to 'news' post type.

Now I when tried to take all the posts from 'meet_our_community' along with the 'news' posts where the meta key 'add_to_meet_our_community' checked with value 'Yes',I am not getting result

    $args = array(
    'post_type'      => array( 'meet_our_community', 'news' ),
    'posts_per_page' => -1,
    'meta_query'     => array(
        'relation' => 'AND',
        array(
            'key'     => 'add_to_meet_our_community', // ACF field key for 'news' post type
            'value'   => 'Yes',
            'compare' => 'LIKE',
        ),
       
    ),
     
    
);

    $combined_query = new WP_Query( $args );

Now I am getting only the correct result from the 'news'custom post only.But I need to get all the posts from 'meet_our_community' along with the news posts. how Can I add meta query that will reflect only on the news post on the WP_Query

2 Answers 2

0

Your query there is looking for results from either post type that have the meta value. AFAIK there's no way to do that in a single query unless you also add the checkbox to the other post type.

3
  • I just need to add the meta query for news post only and get all the posts from 'add_to_meet_our_community' posts. is there any way get it. I am still stuck on it
    – Amal
    Commented May 9 at 11:08
  • Like I said, you could add the checkbox to meet_our_community. Or you could run two queries, one for meet_our_community and one for news with the checkbox and merge the results.
    – Chris Cox
    Commented May 9 at 11:14
  • nice to meet you and thanks for the help. yes. there is an option to run two query's and merge it. But the the requirement I am facing it to in single query, but the meta query not working as expected.else i need to move for sql query. still the meta query 'And' condition is been added to the 'meet_our_community' post that result the issue. Can't able to getting the thing figure out.
    – Amal
    Commented May 9 at 11:21
0
$args = array(
    'post_type' => array('meet_our_community', 'news'),
    'post_status' => 'publish',
    'posts_per_page' => -1,
   
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'add_to_meet_our_community',
             'value' => array('Yes'),
            'compare' => 'LIKE', 
        ),
        array(
            'key' => 'add_to_meet_our_community',
            'compare' => 'NOT EXISTS'
        )
    ),
    'meta_key' => 'date',
    'orderby' => array('date' => 'DESC')
);

this code worked for me

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