0

Currently I use: query_posts("post_type=listing");

to pull all entries of my custom post type. But I have a taxonomy called status with the values of Available, Pending, Sold.

I only want to show Available and Pending on one page, and sold on another.

How do I go about this?

thanks!

1

There's the "tax_query" argument available since the latest wp release:

global $query_string;
$args['tax_query'] = array(
    array(
         'taxonomy' => 'status'
        ,'terms'    => array( 'available', 'pending' ) // change to "sold" for 2nd query
        ,'field'    => 'slug'
    ),
);
$args['post_type'] = 'listing';
parse_str( $query_string, $args );

$avail_n_pend = query_posts( $args );

if ( $avail_n_pend->have_posts() ) : 
    while ( $avail_n_pend->have_posts() ) : 
    $avail_n_pend->the_post();
    // show result
    the_title();
    endwhile; 
endif;

// use this for testing:
/*
echo '<pre>'; 
    print_r($GLOBALS['wp_query']->tax_query); 
echo '</pre>';
*/

// rewind for second query
rewind_posts();

// second_query
$args['tax_query'] = array(
    array(
         'taxonomy' => 'status'
        ,'terms'    => array( 'sold' )
        ,'field'    => 'slug'
    ),
);
parse_str( $query_string, $args );

$sold = query_posts( $args );

if ( $sold->have_posts() ) : 
    while ( $sold->have_posts() ) : 
    $sold->the_post();
    // show result
    the_title();
    endwhile; 
endif;
  • I'm getting an error trying this: PHP Parse error: syntax error, unexpected ':' on the line of code that says: ` if ( $avail_n_pend->have_posts() ) : $avail_n_pend->while ( have_posts() ) : $avail_n_pend->the_post(); ` – shawn Jun 4 '11 at 8:37
  • See Update Then see which line it "really" is and fix it (use the edit button below the Answer) – kaiser Jun 4 '11 at 18:13
  • sorry, I have tried every combination of code changes that I can think of but keep getting errors. That is why I didn't edit the answer. Still trying to learn how to do this properly. – shawn Jun 6 '11 at 8:06
  • 1
    n/p. I dropped it in the IDE and saw the failure. You can use it now. Btw: Download a copy of Eclipse. This will help you a lot. – kaiser Jun 6 '11 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.