Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using this code to display custom post type.

$gabquery = new WP_Query();
$gabquery->query('showposts='.$showpostbotleft.'&post_type=courses' );

How to edit it to limit number of posts and specific taxonomy name of this custom post type.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

I prefer using using the array form...

$gabquery = new WP_Query(array(
    'post_type'=>'courses',//The name of the post type
    'posts_per_page'=>5, //Use this rather than showposts
    'tax_query'=>array(
         array(
          'taxonomy'=>'my-custom-tax', //Your custom taxonomy name
          'operator' => 'IN',//NOT IN & AND also available
          'field'=>'slug',//or you could select by ID
          'terms'=>array('my-term-slug')//Get posts with this term (or ID if above is ID)
         )
    )
));

The above queries posts of type 'courses', limits the number of posts to be displayed to 5 per page, and selects only posts which belong to the 'my-term-slug' term of the 'my-custom-tax' taxonomy.

See the WP_Query Codex.

share|improve this answer
    
It gives this error;Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' –  Ahmad Feb 1 '12 at 11:15
    
Seems there's a slight error in the above code (a missing ')' somewhere...) –  Stephen Harris Feb 1 '12 at 11:28
1  
There was a missing comma, that may have been it. I've updated the code :D. –  Stephen Harris Feb 1 '12 at 11:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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