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

  • 1
    @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, 2011 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. Jan 14, 2011 at 14:23
  • 1
    @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, 2011 at 14:29
  • Comments are not for answers. Just saying. :)
    – Rarst
    Jan 14, 2011 at 14:45
  • I wasn't sure if I can answer my own question. So posted the solution in the comments.
    – Sri
    Jan 14, 2011 at 14:47

1 Answer 1


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(); ?>
  • Seems to have small typo - double single quote after custom taxonomy.
    – Rarst
    Jan 14, 2011 at 15:03
  • Oops. Yeah. I've changed it.
    – Sri
    Jan 14, 2011 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, 2011 at 15:58
  • Where is'sp_events_cat' defined, since it isn't a core wordpress query key? Sep 25, 2013 at 13:01

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.