How to include category name/id in wp_query for retrieving “custom post type” from a particular category?
-
16-10-2019 - |
Question
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
Solution
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(); ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange