0

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

1 Answer 1

1

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

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.