最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - WP_QUERY wrong ammount of posts

matteradmin6PV0评论

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 badges
Add a comment  | 

2 Answers 2

Reset to default 1

But 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;
}
?>

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far