admin管理员组文章数量:1435859
I have 16 published posts of type "portfolio". With the query below, "found_posts" is 16. Correct so far.
I've set "posts_per_page" to -1 to see all of them. But only 8 of them get rendered. The wordpress setting posts per page is 10, so this cant be the issues. There is also no multilingual plugin like WPML working.
What am i doing wrong?
function portfolio_filter(){
$query = new WP_Query( array( 'posts_per_page' => -1,'post_status' => 'publish', 'post_type' => 'portfolio') );
$output = $query->found_posts; // Returns 16
if ( $query->have_posts() ) :
while ( $query->have_posts() ) {
$query->the_post();
$output.='<div class="entry filter_product">';
$output.=get_the_post_thumbnail($query->the_post()->ID,'medium');
$output.='<h3 class="title">'.get_the_title().'</h3>';
$output.='</div>';
}
wp_reset_postdata();
else :
//show 404 error here -->
endif;
return $output;
}
I have 16 published posts of type "portfolio". With the query below, "found_posts" is 16. Correct so far.
I've set "posts_per_page" to -1 to see all of them. But only 8 of them get rendered. The wordpress setting posts per page is 10, so this cant be the issues. There is also no multilingual plugin like WPML working.
What am i doing wrong?
function portfolio_filter(){
$query = new WP_Query( array( 'posts_per_page' => -1,'post_status' => 'publish', 'post_type' => 'portfolio') );
$output = $query->found_posts; // Returns 16
if ( $query->have_posts() ) :
while ( $query->have_posts() ) {
$query->the_post();
$output.='<div class="entry filter_product">';
$output.=get_the_post_thumbnail($query->the_post()->ID,'medium');
$output.='<h3 class="title">'.get_the_title().'</h3>';
$output.='</div>';
}
wp_reset_postdata();
else :
//show 404 error here -->
endif;
return $output;
}
Share Improve this question edited Apr 1, 2019 at 8:08 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Apr 1, 2019 at 7:59 Pat_MoritaPat_Morita 1897 bronze badges2 Answers
Reset to default 1But all of them get obtained from the database.
The problem is that you’re ignoring half of them. Or rather merging two of them and displaying as one.
Let’s take a look at your code:
while ( $query->have_posts() ) {
$query->the_post(); // <- here you call the_post() first time
$output.='<div class="entry filter_product">';
// and in the next line you call the_post second time
$output.=get_the_post_thumbnail($query->the_post()->ID,'medium');
$output.='<h3 class="title">'.get_the_title().'</h3>';
$output.='</div>';
}
Every time you call the_post method, you tell the loop to go to the next post. So if you call the_post twice in one loop, then you’ll be skipping by two posts, not by one.
You should change this line:
$output.=get_the_post_thumbnail($query->the_post()->ID,'medium');
To this:
$output.=get_the_post_thumbnail(get_the_ID(),'medium');
Hope this code may help you.
<?php
function portfolio_filter(){
$args = array(
'post_type' => 'portfolio',
'post_status' => 'publish',
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while($query->have_posts()) : $query->the_post(); ?>
<div class="entry filter_product">
<?php the_post_thumbnail( 'medium' ); ?>
<h3 class="title"><?php echo get_the_title(); ?></h3>
</div>
<?php endwhile;
wp_reset_postdata();
else : ?>
<p>No Posts Found.</p>
<?php endif;
}
?>
本文标签: wp queryWPQUERY wrong ammount of posts
版权声明:本文标题:wp query - WP_QUERY wrong ammount of posts 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745640740a2667840.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论