I have trouble in getting post in a custom post type category. I have code below but it doesnt work well. It still get posts in another category.
<?php
$query= null;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array(
'post_type' => get_post_type(),
'post_status' => 'publish',
'paged' => $paged,
'post_type_cat' => 'featured', // get_post_type() will return post_type, I add _cat -> post_type_cat
//'orderby' => 'rand',
'posts_per_page' => 1,
'meta_query' => array(
array(
'key' => '_expiration_date',
'value' => array(0, current_time('timestamp')),
'compare' => 'BETWEEN'
)),
);
$query = new WP_Query($args);
?>
<?php if ( $query->have_posts() ) : $query->the_post(); ?>
<?php get_template_part( 'template/featured' ); ?>
<?php else : ?>
<?php get_template_part( 'template/nofeatured' ); ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
Can you help me?
Thank you
As far as I know there is no such parameter as
post_type_cat
, you want to use eithercat
or if querying posts in a custom taxonomy you would use a taxonomy query.Category query example;
or
See the following Codex entry for even more ways to query by category;
http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
Taxonomy query example;
See this entry for more details:
http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
I could not for the life of me get any of the above to work. They either brought back everything or nothing. So after a bit of trial and error, I did this:-
id
is the slug of the category I want.I’m new to WordPress, having programmed just about every other language, and I have no idea if this is a good or bad way to do things I just know it works for my needs 🙂