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) ?
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) ?
Share Improve this question edited Mar 11, 2020 at 15:15 Knownow asked Mar 11, 2020 at 14:13 KnownowKnownow 1216 bronze badges 5 |3 Answers
Reset to default 0The 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
}
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();
}
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');
$post
in the function call to$posts
in theif()
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:49WP_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