I'm trying to display a set of posts with the thumbnail, title, and content/excerpt. I don't want to show posts that don't have content or a thumbnail for display/formatting purposes since this is only going to be shown on my homepage.

Here is a live example of working version of what I want it to look like.

My problem is that I have some posts that are only shortcodes (no real content) or just an image (no text on the page).

The shortcode problem I was able to figure out by using:

$content = strip_shortcodes( get_post()->post_content );
if( '' !== $content ) 

Then I tried taking the above a step further to remove posts that only had an image and not text. So like this:

$content = strip_shortcodes( get_post()->post_content );
$content = str_word_count( strip_tags( $content ) );
if( '' !== $content )

This didn't seem to have any effect at all. The posts were still displayed the same they were with the previous example.

Any help on what I'm doing wrong here or how I should go about doing this? How would I check that the post has actual content, as in text/words?

Here's my complete code below:


$args = new WP_Query(array(
    'posts_per_page' => 5,
    'post__in'       => get_option( 'sticky_posts') ,
    'meta_query' => array(
            'key'     => '_thumbnail_id',
            'compare' => 'EXISTS',
    'post_status'    => 'publish',
    'orderby'        => 'post__in',
    'post_type'      => array( 'post' ),
    'ignore_sticky_posts' => true,
$the_query = new WP_Query( $args ); 

if ( $the_query->have_posts() ) {

  while ( $the_query->have_posts() ) {

  $content = strip_shortcodes( get_post()->post_content );
  $content = str_word_count( strip_tags( $content ) );

  if( '' !== $content && has_post_thumbnail() ): ?>

      <article class="thumbnail item" itemscope="" itemtype="http://schema.org/CreativeWork">
        <a class="blog-thumb-img" href="<?php the_permalink(); ?>" title="Read more">
          <?php the_post_thumbnail(); ?>
        <div class="caption pt30">
          <h4 itemprop="headline">
            <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
          <p itemprop="text" class="flex-text text-muted"><?php the_excerpt();?></p>

  <?php endif;



1 Answer 1


Here are some notes:

  • There's a problem with how $args is defined:

    $args = new WP_Query(array( ... ) );
    $the_query = new WP_Query( $args ); 

    i.e. there are two WP_Query instances here but $args must be an array.

  • It looks like this part is problematic too:

    $content = str_word_count( strip_tags( $content ) );
    if( '' !== $content )

    Note that str_word_count() returns a number or an array for other format input parameters.

    Here you're using a strict empty string comparison.

  • You should use pre_get_posts action if possible, instead of introducing another secondary query.

  • I can see another possible problem with this approach: If the query returns only posts that contains shortcodes or images, then there's nothing to display.

  • Another approach might be to automatically store that information (e.g. as post meta) when you save the post.

    If you store it under the post meta key _wpse_searchable then you could adjust your meta query:

    'meta_query' => [
       'relation' => 'AND',
            'key'     => '_thumbnail_id',
            'compare' => 'EXISTS',
            'key'     => '_wpse_searchable',
            'value'   => true

    If a post isn't "searchable", next time we update it, we could also just delete that custom field automatically.

  • Thanks @birgire! I should have checked on str_word_count() beforehand. That fixed the issue. However, I'm a little confused about the other stuff you suggest. The way I'm doing it is not ideal? Also, I realized the meta query search for thumbnail_id doesn't do anything, which is why I was using has_post_thumbnail() as well. Is this one of the issues you were referring to? Commented Aug 7, 2016 at 1:16
  • You're welcome.I updated the answer regarding some other issues @BryanWillis..
    – birgire
    Commented Aug 7, 2016 at 10:59
  • 1
    Hah wow can't believe I didn't notice that. Classic case of staring at a computer screen too long. Thanks again. Commented Aug 8, 2016 at 4:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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