admin管理员组

文章数量:1125605

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.

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.

Share Improve this question asked Apr 27, 2016 at 7:07 deemi-D-nadeemdeemi-D-nadeem 5672 gold badges8 silver badges26 bronze badges 10
  • and if you use 10 instead of 999? why do you think it has anything to do with the query at all? – Mark Kaplun Commented Apr 27, 2016 at 7:16
  • I want infinite data of non featured post – deemi-D-nadeem Commented Apr 27, 2016 at 7:17
  • and nothing happen when i convert 999 to 10 – deemi-D-nadeem 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
 |  Show 5 more comments

2 Answers 2

Reset to default 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);

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' 
                          ),
                        )
                );

本文标签: custom fieldmetaquery 39compare39 gt 3939 not working