if the post has content

I am setting up a one page WordPress site. I am getting some pages listed in my site that does not have content. For example, I will get the empty blog page as well as the blog template. So I thought I could throw in a check to see if the page has content and if it does go ahead and post that information. I am having trouble getting it to work. I am using a custom query for the homepage. So I thought I could do this

 if ( $page_query->have_posts() ) : while ( $page_query->have_posts() ) : $page_query->the_post();
 if( $page_query->post_content != ''){
       get_template_part( 'content', get_post_format() );
 }
 endwhile; endif;

problem is that I get an error on that code and I can’t figure out why. I get this error

Notice: Undefined property: WP_Query::$post_content in

4

The content is a property of the post object, not of the query object.

Use $post or get_post() instead:

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

Leave a Comment