admin管理员组文章数量:1125064
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
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
Share Improve this question asked May 7, 2013 at 4:29 Kien PhamKien Pham 771 gold badge1 silver badge6 bronze badges 1- What is the category name? – montrealist Commented May 7, 2013 at 4:59
3 Answers
Reset to default 8As far as I know there is no such parameter as post_type_cat
, you want to use either cat
or if querying posts in a custom taxonomy you would use a taxonomy query.
Category query example;
$query = new WP_Query( 'cat=2,6,17,38' );
or
$query = new WP_Query( 'category_name=staff' );
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;
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'people',
'field' => 'slug', //can be set to ID
'terms' => 'bob' //if field is ID you can reference by cat/term number
)
)
);
$query = new WP_Query( $args );
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.
if (isset($_GET["id"])) {
$id = $_GET["id"];
echo $id;
$id = get_category_by_slug( $_GET["id"] );
if (isset($id)) {
$id = $id->term_id;
$args = array(
'post_status' => 'publish',
'post_type' => 'Products',
'cat' => $id,
);
}
}
$query = new WP_Query($args);
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 :)
I know this is old, but I wasn't able to find a clear answer. So here is what worked for me:
WP_Query version:
$args = array(
"post_type" => "story",
"story-category" => $category->slug
);
$query = new WP_Query( $args );
get_posts version:
$stories = get_posts([
"post_type" => "story",
"story-category" => $category->slug
]);
It's as simple as that. Just need to pass the custom post type, and then the category slug in a parameter with the same name as the custom post type category slug. I hope this makes sense :)
本文标签: wp queryGet Posts in a Custom Post Type category
版权声明:本文标题:wp query - Get Posts in a Custom Post Type category 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736654917a1946228.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论