0
votes

I have this query to get posts with custom field, which works perfectly.

<?php query_posts('meta_key=version&meta_value=one');  ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
// my posts displaying 

Now I wants to get posts which does not have meta_value to 'one' or does not have version meta_key set. I tried with empty strings 'meta_key=&meta_value= ' but it return all posts including which has meta_value to 'one'.

Thanks for any help

1

1 Answers

2
votes

Use $the_query = WP_Query($params). It returns a query object that can be used like so:-

while ( $the_query->have_posts() ) : $the_query->the_post();
    echo '<li>';
    the_title();
    echo '</li>';
endwhile;

$params = array(
        'meta_query' =>array(

                                array(
                                        'key' => 'version',
                                        'value' =>'one',
                                        'type' => 'char',
                                        'compare' => 'NOT'),
                            ));

$the_query = new WP_Query($params);

further information on what operators you can use here