12

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 Answers 4

24

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
}
0
8

What about

if ( !empty( get_the_content() ) ){ 
//code 
}
3
  • You can not pass a function to empty() as a variable. You must first store it in a variable. Even then, it won't work, since there might be some empty spaces in your content.
    – Johansson
    Commented Sep 10, 2017 at 20:14
  • 2
    This is working for me! are you sure? At least in PHP 7 Commented Sep 11, 2017 at 8:04
  • @Johansson Not sure what did you get that from or if I'm missing something, but of course you can, because get_the_content() returns a string which may or may not be empty. Not to confuse with the_content(), which echoes a string. Commented Apr 3 at 8:22
5

i have implemented some "has_content()" methods for several times over years now and there is always enough time in between so i need to search again a bit to answer this question.

anyways - this is my solution, which i like to find the next time here - so its for reference.

all "inside loop" functions can be replaced by a post objects "post_content"

in functions.php and similar files:

// write inside the loop
$the_content = apply_filters('the_content', get_the_content());
if ( !empty($the_content) ) {
  echo $the_content;
}
// with post object by id
$post = get_post(12); // specific post
$the_content = apply_filters('the_content', $post->post_content);
if ( !empty($the_content) ) {
  echo $the_content;
}

as function

// call inside the loop
function mytheme_has_content(){
  return !empty(apply_filters('the_content', get_the_content()));
}

template inside the loop:

<?php if ( $customQuery->have_posts() ) {?>
  <?php while ( $customQuery->have_posts() ) {
    $customQuery->the_post(); ?>
    <?php $the_content = apply_filters('the_content', get_the_content()); ?>
    <!-- html -->
    <?php if ( !empty($the_content) ) { ?>
      <div class="content">
        <?php echo $the_content; ?>
      </div>
    <?php } ?>
  <?php } ?>
  <?php wp_reset_postdata(); ?>
<?php } ?>
5
  • 1
    this is the second time i consult this, i will leave one comment every time now i come back for statistik purpose Commented Mar 10, 2020 at 7:59
  • third time here Commented Aug 19, 2020 at 5:52
  • 1
    4th its now 2020 Commented Oct 18, 2020 at 10:28
  • 1
    5th at 2021 seems like once a year Commented Mar 23, 2021 at 15:45
  • 1
    6th again 2021... Commented Nov 26, 2021 at 9:03
2

This also works, and tests for things like empty paragraph tags or &nbsp; in the content which might cause a normal check to fail. See http://blog.room34.com/archives/5360 for the original idea - just recording it here so I can find it again. :O)

Put this in your functions.php:

function empty_content($str) {
    return trim(str_replace('&nbsp;','',strip_tags($str))) == '';
}

And put this where you want to run the check:

if (function_exists('empty_content') && empty_content($post->post_content)) { ... }

That will return true if the content is empty, false if not.

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.