0

I have a custom post type 'listings' and one of its taxonomies is 'status'. I want to create two widgets:

  1. display all 'listings' WITH 'status' 'sold'.
  2. display all 'listings' WITHOUT 'status' 'sold'.

I've achieved the first widget using

query_posts( array(
    'status' => 'sold' )
);

I can't create the second widget. It should be like "status => !sold", or exclude sold. Any ideas?

2 Answers 2

2

Try below code when you need status = sold

$args = array(
    'post_type' => 'listing',
    'meta_query' => array(
        array(
            'key' => 'status',
            'value' => 'sold',
            'compare' => 'LIKE'
        )
    )
);
$myQuery = new WP_Query($args);

And below code when you want status != sold

$args1 = array(
    'post_type' => 'listing',
    'meta_query' => array(
        array(
            'key' => 'status',
            'value' => 'sold',
            'compare' => 'NOT LIKE'
        )
    )
);
$myQuery1 = new WP_Query($args1);
1
  • Upvoted your answer for the proper use of WP_Query Aug 13, 2014 at 7:30
0

This works perfectly...

query_posts( array(
    'post_type' => 'listings',
    'tax_query' => array(
        array(
            'taxonomy' => 'status',
            'field'    => 'slug',
            'terms'    => 'sold',
            'operator' => 'NOT IN'
            ),
        )
    )
);

This code excludes status => sold from post_type => listings

2
  • 1
    NO NEVER EVER use query_posts. It is resource intensive, is slow, breaks pagination and breaks the main query. Rather use WP_Query Aug 13, 2014 at 7:30
  • I use <?php wp_reset_query(); ?> for the main query. I can also use posts_per_page if I wanted pagination. Although I'm not quite sure about the "resource intensive and slow" thingy... some forum said the performance is the same and that the only difference is that it replaces the main query. Aug 13, 2014 at 9:16

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.