Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I don't want to display post with empty body / post_content on my home page. So I added below code to my function.php. It detects post with empty body but it still display them. I expected that if I return '' the post won't be displayed.

  • How can I remove post from displaying?
  • How does the filter the_posts work?

The code:

function remove_post_with_empty_body ( $posts ) {

    if (($posts->post_content) == '') { 


        echo 'empty'; //also tried return false; and return null;

        return '';
    }
    else {

        echo 'not empty';
        return $posts;
    }

}
add_action('the_post', 'remove_post_with_empty_body');
share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

First thing, in your code you are using the_post hook but in your question you are asking about the_posts hook, which are two different things.

the_posts gets called just after the posts have been selected from the database and it passes an array of $posts to your function, so you should use that.

as for the_post hook, it gets fired (usually) inside the loop it self which is to late to change any thing (like redirect) and its not a filter hook, but an action hook which means that if you return nothing, its just exiting your function and not effecting the outcome.

share|improve this answer
    
Thank you for the answer and explanation. –  Radek Jan 2 '12 at 9:35
    
Glad i could help :) –  Bainternet Jan 2 '12 at 9:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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