0

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;

}

2

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');
| improve this answer | |
0

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;
}
?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.