Query all post and CPT from 2 specific taxonomies AND by ACF custom field

I need to build a complex query. I want to display all posts, and 2 different CPT posts in specific taxonomies. All of these posts need to have been checked to be displayed. The checkbox is added with ACF. My ACF checkbox is named highlight and the value needs to be yes.

So my query should display:

  • all posts of all categories but with the ACF checkbox checked
  • all post of the CPT1 with the taxonomy TAX1 and ACF checkbox checked
  • all post of the CPT2 with the taxonomy TAX2 and ACF checkbox checked

So far I tried :

query_posts(array(
            'post_type' = array(  'post', 'cpt1', 'cpt2' ),
             'post_status' = 'publish',
             'orderby' = 'date',
             'order' = 'DESC',
             'posts_per_page' = 10,
             
             'meta_query' = array(
                'relation' = 'AND',
                 array(
                     'key'     = 'highlight',
                     'value'   = 'yes',
                     'compare' = 'LIKE'
                 )
                ),
                 'tax_query' = array(
                    'relation' = 'OR',
                    array(
                        'taxonomy' = 'TAX1',
                        'field'    = 'slug',
                        'terms'    = 'valueTAX1',
                    ),
                    array(
                        'taxonomy' = 'TAX2',
                        'field'    = 'slug',
                        'terms'    = 'valueTAX2',
                    ),
                   
                ),
         )

It's not working though :(

Any help would be so appreciated!

Thank yoou, Sandra

Topic multi-taxonomy-query wp-query taxonomy custom-post-types Wordpress

Category Web


Finally I found a solution!

Here is the query in case someone needs it :

 // first query
            $first_ids = get_posts(array(
                'fields'         => 'ids',
                'posts_per_page' => '10',
                'post_status'    => 'publish',
                'post_type'      => array('post'),
                'orderby'        => 'date',
                'order'          => 'DESC',
              
            ));

            // second query
            $second_ids = get_posts(array(
                'fields'         => 'ids',
                'posts_per_page' => '10',
                'post_status'    => 'publish',
                'post_type'      => array('CPT1'),
                'orderby'        => 'date',
                'order'          => 'DESC',
                'tax_query'      => array(array(
                    'taxonomy'       => 'TAX1',
                    'field'          => 'slug',
                    'terms'          =>  'tax_value',
                ))
            ));

            // third query
            $third_ids = get_posts(array(
                'fields'         => 'ids',
                'posts_per_page' => '10',
                'orderby'        => 'date',
                'order'          => 'DESC',
               
                'post_type'      => array('CPT2'),
               
                'tax_query'      => array(array(
                    'taxonomy'       => 'TAX2',
                    'field'          => 'slug',
                    'terms'          => 'TAX2value,
                ))
            ));

            // merging ids
            $post_ids_list = array_merge($first_ids, $second_ids, $third_ids);

            $querySlider = new WP_Query(array(
                'post_type' => 'any',
                'posts_per_page' => '10',
                'post__in'  => $post_ids_list,
                'post_status'    => 'publish',
                'orderby'   => 'date',
                'order'     => 'DESC',
                'meta_query' => array(
                    array(
                        'key'     => 'highlight',
                        'value'   => '"yes"',
                        'compare' => 'LIKE'
                    )
                ),
            ));
            ?>
           

I found the solution here : Merge 2 args in one WP_Query and order it by date

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.