1

I'm trying to create a new WP_Query that displays the last 5 posts from cat 1 that doesn't have a thumbnail. This actually works:

<?php
$the_query = new WP_Query( 'cat=1&posts_per_page=5' );
while ( $the_query->have_posts() ) :
    $the_query->the_post(); if ( ! has_post_thumbnail() ) { ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php } endwhile; 
wp_reset_query();
wp_reset_postdata(); 
?>

But let's say of the last 5 posts, 3 have the thumbnail; in this case, only 2 posts are displayed. I'd like to have 5 as fixed number of posts_per_page and if in the loop end up posts with thumbnail the older posts are displayed (to reach the number of 5), instead of displaying just the 2 without thumbnails.

I tried putting the if before $the_query but it didn't work.

  • how many posts do you have in your category? Do you have more than two that don't have a featured image? – Jamie Dec 27 '14 at 1:51
  • yep. the problem of my code is that the function gets the last 5 posts and then the filter of the thumbnail is applied. So if 3 posts of these 5 have the thumbnail only 5-3=2 are displayed. The filter should be applied before I think, so the 5 posts selected by posts_per_page are already the ones without the thumbnail. – MultiformeIngegno Dec 27 '14 at 1:53
  • try using next_post(); So you could try. if !has_post_thumbnail() do content else $the_query->next_post(); I don't know if that would work. I have never used it – Jamie Dec 27 '14 at 2:08
  • Where? And it seems deprecated.. – MultiformeIngegno Dec 27 '14 at 2:09
  • Maybe they changed it to codex.wordpress.org/Function_Reference/get_next_post – Jamie Dec 27 '14 at 2:15
1

You can try to fetch posts, that don't have a featured image, with:

$ppp = 5; // posts per page

/**
 * Fetch $ppp posts, without thumbnails, in a given category:
 */
$args = array(
    'cat'            => 1,
    'posts_per_page' => $ppp,
    'meta_query' => array(
        array(
            'key'     => '_thumbnail_id',
            'compare' => 'NOT EXISTS',
        ),
    ),
);
$myposts = get_posts( $args );

But what if there are not enough posts, without a featured image, in that category? Well, then we can just fill the rest with posts with a featured image:

/**
 * Fetch $ppp-x posts in a given category, with thumbnails, if x > 0:
 */
if( $count = count( $posts ) < $ppp ) 
{  
    $args = array(
        'cat'            => 1,
        'posts_per_page' => $ppp - $count,
        'meta_query' => array(
            array(
                'key'     => '_thumbnail_id',
                'compare' => 'EXISTS',
            ),
        ),
    );
    $myposts = array_merge( $posts, get_posts( $args ) );
}

Then you can setup your loop with:

global $post;
foreach( $myposts as $post ) 
{
    setup_postdata( $post );
    ?><li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li><?php
}
wp_reset_postdata();

You might also want to make a check if you got enough posts within the corresponding category.

Notice that this is untested, but I hope you can modify this to your needs.

ps: If I remember correctly, @kaiser already solved something similar in the general case, with some extra flavors added ;-)

I will add a link if I find it.

Update: Found it here, I think. There are some interesting answers over there.

  • Thanks! Obviously this is the way to go. Unfortunately I tried your snippet but the output is blank.. – MultiformeIngegno Dec 27 '14 at 16:59
  • It's because I have another query_posts in the same page. I digged in WordPress codex but I'm not able to have them working at the same time.. Here's what I tried: pastebin.com/q1a9heG7 The first query is displayed. the second is blank.. – MultiformeIngegno Dec 27 '14 at 17:08
  • Hi @MultiformeIngegno. Blank page means you probably got a PHP syntax error. I just tried the above code and it worked well on my install. I can see in your pastebin that you're using query_posts(), please don't use that. It's not recommended. Check for example this note in the Codex: This function isn't meant to be used by plugins or themes... – birgire Dec 27 '14 at 17:39
  • Thanks. I followed your advice and it worked like a charm now replacing also the other function with get_posts. :) – MultiformeIngegno Dec 27 '14 at 18:56
  • Glad to hear it worked for you. The problem with query_posts() is that it's modifying the main query, which can lead to all kinds of problem. The get_posts() function is very handy sometimes, since it's just a simple wrapper for the WP_Query() function. – birgire Dec 27 '14 at 19:01

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.