0

In my blog index page I do not want to show posts which has empty content but only title (How does filter the_posts work?) . I am looking at 'the_post' hook which seems to fire inside the loop . So , I thought if the action hook is fired inside the loop I could check the post content on 'the_post' hook and if it is null continue with the loop . I tried the below :

add_action('the_post' , 'rb_test_the_post');

function rb_test_the_post($post){
    if (($post->post_content) == '') { 
        continue;
    }
} 

This gives me an error in Wordpress ( "The site is experiencing technical difficulties " and also in my editor as it doesnt see any while or if statement for the continue statement.) . I am not able to understand why the continue statement doesnt work even though the hook is fired inside the loop .Could some one please explain?

Also , how would one achieve this (using a plugin and not the theme as I want the empty posts not to show even when I change theme) ?

5
  • You've changed from $post in the function call to $posts in the if() statement. As far as the function is concerned, $posts is an uninitialized variable, and you're using it as though it's an object.
    – Pat J
    Commented Mar 11, 2020 at 14:49
  • I changed the code in the if statement (which I happened to somehow mistype in SE) but the problem still persists . Get The site is experiencing technical difficulties error .
    – Knownow
    Commented Mar 11, 2020 at 15:17
  • I'd recommend turning on WP_DEBUG to see what the actual error is. The "WordPress is experiencing technical difficulties" error is a generic message that usually indicates there's a fatal error somewhere in your PHP code.
    – Pat J
    Commented Mar 11, 2020 at 16:46
  • 3
    The error is simple your are trying to use continue not within a php loop but within a function, that will not work.
    – Tofandel
    Commented Mar 11, 2020 at 16:51
  • I feel so stupid thanks a lot .
    – Knownow
    Commented Mar 12, 2020 at 1:35

3 Answers 3

0

The content is a property of the post object, not of the query object.
Use $post or get_post() :

  if( '' !== get_post()->post_content ) {
    // do something
    }
0

Just check the post content is empty or not:

if ( "" === $post->post_content )
{
//dont display your post 
 }
else
{
 //your post's code goes here
  the_title();
  the_content();
 }
0

I wanted to do this using a plugin and not theme . Here is how :

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

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.