1

I am using a custom post-type, in post there are a checkbox meta field for featured post.

So there are two scenarios.

  1. First with featured posts
  2. Second with non featured

When I called featured posts with meta_query its result 100% perfect.

Here is my code:

$c_clients_args = array(
                    'post_type' => 'clients_testimonials',
                    'posts_per_page' => 6,
                    'order' => 'ASC',
                    'meta_query' => array(
                                        array(
                                            'key' => 'c_client_feature',
                                            'value' => 'on'
                                        )
                                    )
                );

$c_clients_result = new WP_Query($c_clients_args);

if($c_clients_result->have_posts()) :
    while($c_clients_result->have_posts()) :
         $c_clients_result->the_post();

         // My result
    endwhile;
endif;

But when I call non featured posts with meta_query the Page goes blank.

Here is my code:

$c_client_args = array(
                    'post_type' => 'clients_testimonials',
                    'posts_per_page' => 999,
                    'order' => 'ASC',
                    'meta_query' => array(
                                        array(
                                            'key' => 'c_client_feature',
                                            'value' => 'on',
                                            'compare' => 'NOT LIKE'
                                        )
                                    )
                );

I also tried != and NOT IN

So please guide me.

10
  • and if you use 10 instead of 999? why do you think it has anything to do with the query at all? Commented Apr 27, 2016 at 7:16
  • I want infinite data of non featured post Commented Apr 27, 2016 at 7:17
  • and nothing happen when i convert 999 to 10 Commented Apr 27, 2016 at 7:19
  • There is not "infinite" data in WordPress, but if you want to get all posts that match the query, use nopaging => true and posts_per_page => -1.
    – cybmeta
    Commented Apr 27, 2016 at 7:44
  • I don't see anything wrong in your code. Are you sure that c_client_feature meta field exists for non featured posts?
    – cybmeta
    Commented Apr 27, 2016 at 7:46

2 Answers 2

1

Issue with your second query for non-featured posts is that 'NOT LIKE' and '!=' comparisons might not be working as expected for a checkbox field. For checkbox fields in WordPress, you should generally use 'NOT EXISTS' or '!= 1' in the meta_query

try this second query

$c_client_args = array(
    'post_type'      => 'clients_testimonials',
    'posts_per_page' => 999,
    'order'          => 'ASC',
    'meta_query'     => array(
        array(
            'key'     => 'c_client_feature',
            'value'   => 'on',
            'compare' => 'NOT EXISTS', // Use 'NOT EXISTS' for checkbox fields
        ),
    ),
);

$c_client_result = new WP_Query($c_client_args);
0

try this second query

  $c_client_args = array(
                    'post_type' => 'clients_testimonials',
                    'posts_per_page' => 999,
                    'order' => 'ASC',
                    'meta_query'  => array(
                         'relation' => 'OR',
                          array(
                             'key' => 'c_client_feature',
                             'compare' => 'NOT EXISTS' 
                          ),
                          array(
                             'key' => 'c_client_feature',
                             'value' => 'on',
                             'compare' => 'NOT IN' 
                          ),
                        )
                );
3
  • noting happend bro :( Commented Apr 27, 2016 at 11:34
  • use wp_reset_query(); after end of loop Commented Apr 27, 2016 at 11:36
  • comment all code your first query which is working and check second is working ? means remove first query for testing purpose Commented Apr 27, 2016 at 11:38

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.