WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here is the code I'm using right now. It fetches the posts from all the categories in the 'sp_events' post type.

<?php $feat_art = new WP_Query(array('post_type' => 'sp_events','post_status' => 'publish','posts_per_page' => 1));   while($feat_art->have_posts()) : $feat_art->the_post(); ?>

I want to display the posts only from a category named 'eventcat1'. I tried the following code, but it did not work.

<?php $feat_art = new WP_Query(array('post_type' => 'sp_events','category_name'=> 'eventcat1','post_status' => 'publish','posts_per_page' => 1));   while($feat_art->have_posts()) : $feat_art->the_post(); ?>

How to specify either the category name or the id in the query? Any help would be appreciated. Thanks

share|improve this question
@Sri: That's weird, category_name is the correct way to do it. Silly thing to ask: you are sure there is at least one custom post that is attached to the eventcat1 category, and that eventcat1 is a regular category, not another custom taxonomy term? (If you reply, please use @Jan so I get a notification) – Jan Fabry Jan 14 '11 at 14:14
@Sri once you've followed Jan's advice if the problem remains then it would be worth you posting your code where the sp_events post type is registered. – sanchothefat Jan 14 '11 at 14:23
@Jan Thanks for pointing it out. The custom post type was registered by the plugin 'Event Calendar Pro' . The Category was a custom taxonomy. Replacing 'category_name' by 'sp_events_cat' made it work. Thanks again. ;) – Sri Jan 14 '11 at 14:29
@sanchothefat Thanks. The issue has been solved. – Sri Jan 14 '11 at 14:40
Comments are not for answers. Just saying. :) – Rarst Jan 14 '11 at 14:45
up vote 2 down vote accepted

The following code worked.

<?php $feat_art = new WP_Query(array('post_type' => 'sp_events','sp_events_cat'=> 'eventcat1','post_status' => 'publish','posts_per_page' => 1));   while($feat_art->have_posts()) : $feat_art->the_post(); ?>
share|improve this answer
Seems to have small typo - double single quote after custom taxonomy. – Rarst Jan 14 '11 at 15:03
Oops. Yeah. I've changed it. – Sri Jan 14 '11 at 15:04
looks fine now. :) I could have edited that myself but decided to poke so you don't end up with that bit broken code stored somewhere. – Rarst Jan 14 '11 at 15:58
Where is'sp_events_cat' defined, since it isn't a core wordpress query key? – emeraldjava Sep 25 '13 at 13:01

Your Answer


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.