Hi i need to display list of post by post type and category, i have code like this, but it isint working properly:
<?php $catquery = new WP_Query( 'posts_per_page=999&post_type=posttypename&cat=categoryname' ); while($catquery->have_posts()) : $catquery->the_post(); $i = 1; ?>
This code displaying post from "posttypename", but it displays all post from that custom post type, but i need to displaying post from only "categoryname"
The whole code looks like this:
<?php $catquery = new WP_Query( 'posts_per_page=999&post_type=posttypename%cat=categoryname' ); while($catquery->have_posts()) : $catquery->the_post(); $i = 1; ?> <?php if($i == 1) : ?> <div class="">content of the post</div> <?php endif; ?> <?php $i++; endwhile; ?>
Use WP_Query like this :
$catquery = new WP_Query(array(
'post_type' => 'posttypename',
'posts_per_page' => -1,
'category_name' => 'categoryname',
// 'cat' => cat ID here
));
if( $catquery->have_posts() ){
while($catquery->have_posts()){
$catquery->the_post();
// your stuff
}
wp_reset_postdata();
}
Try below code
<?php
$catquery = new WP_Query(array(
'post_type' => 'posttypename',
'posts_per_page' => -1,
'category_name' => 'categoryname'
));
if( $catquery->have_posts() ){
$i = 1;
while($catquery->have_posts()){
if($i == 1){ ?>
<div class="">content of the post</div>
<?php } ?>
$i++;
}
wp_reset_postdata();
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.