[resolved] WordPress Loop - skip posts without a thumbnail (2 posts)

  1. Sascha
    Posted 2 years ago #

    I want skip every post that has no thumbnail. The code does not work properly yet.

    Actually the script doesn't show posts without a thumbnail - that's good, but in the loop the post with no thumbnail is still counted as a post.

    So when i have for example 10 posts in my wordpress database. I want show 5 of them. But only the posts who has a thumbnail.

        $args = array(  'numberposts'  => 5,
                        'orderby'      => 'date',
                        'order'        => 'DESC',
                        'post_type'    => 'post',
                        'post_status'  => 'publish'
        $my_posts = get_posts( $args );
        global $post;
        foreach( $my_posts as $post ) : setup_postdata($post); 
        if ( !has_post_thumbnail() ) {
        } else {
                <div class="clearfix" >
                    <div class="thumb"><?php the_post_thumbnail('post-image-big'); ?></div>
                    <a href="<?php the_permalink(); ?>" class="title"><?php the_title(); ?></a>
                    <p class="category"><?php the_category(', '); ?></p>
        <?php } ?>
        <?php endforeach; ?>

    Simple add this
    'meta_key' => '_thumbnail_id'

  2. Girmay
    Posted 1 year ago #

    You're almost there...
    you will need to rewind the loop count right before you continue to the next post... get your current position first...$wp_query->current_post

Topic Closed

This topic has been closed to new replies.

About this Topic