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

本文标签: wp queryWPQUERY wrong ammount of posts