Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WP-Query : check if the post content is empty

Tags:

php

wordpress

I'm trying to check if my post has content, in the loop. Currently, i added a condition in the loop :

if ( $post->post_content ) 

and putted argument in the

wp query ('posts_per_page' => 8).

I thought it worked, but actually, WP query go find last 8 posts, and check the content of those 8 lasts. So it renders 2 or 3 posts.

The thing I want is a way to show last 8 posts that has content.

See what I mean ?

I really appreciate some help :)

Best regards.

like image 789
enguerranws Avatar asked Oct 22 '25 05:10

enguerranws


1 Answers

This isn't possible with a standard WP query, and you'll have to leverage the use of posts_where before the WP_Query is called.

function filter_where($where = ''){
    return $where .= "AND trim(coalesce(post_content, '')) <>''";
}

In the above, we're simply only selecting posts where the column post_content isn't empty.

Then add the filter.

add_filter('posts_where', 'filter_where');

Now perform the query.

$query = new WP_Query(array('post_type' => 'post', 'posts_per_page' => 8));

And then when you're done, remove the filter from the query so it doesn't interfere.

remove_filter('posts_where', 'filter_where');
like image 74
Ohgodwhy Avatar answered Oct 23 '25 21:10

Ohgodwhy