WordPress query to show only posts where content is not empty

I am using a query to show only 1 custom post type randomly on my home page. I’m trying to filter out the posts where the content is empty so as to not show those posts in the loop. Is it possible to set the query to only show post that “post_content” is not empty? I’ve tried “meta_query” with no luck. As covered here.

I have also played around with other things, like trying to get the next post in the loop if the content is empty, but can’t find a way to do that either.

Read More

Here is something I was trying to get the next post, if content is empty. But I don’t think “get_next_post();” works that way.

$loop = new WP_Query( array(
        'post_type' => 'custom',
        'post_status' => 'publish', 
        'posts_per_page' => 1,
        'orderby' => 'rand',
        'order' => 'DESC',
    ) );
while ( $loop->have_posts() ) : $loop->the_post();

if($post->post_content=="") {
get_next_post();
} else {
the_title();
the_content();
};
endwhile;

Any suggestions would be appreciated.

Related posts

1 comment

  1. Something basic like this should work for you. Trimming the post content first ensures that you’re not inadvertently including posts with nothing but whitespace.

    while ( $loop->have_posts() ) : $loop->the_post();
        if(trim($post->post_content) !== "") {
            the_title();
            the_content();
        };
    endwhile;
    

Comments are closed.