I need to be able to do something if the content is empty in a CPT. This works for standard WP posts using the following:

if($post->post_content=="") {

//Do something if there is content is empty  

} else {

//Do something if there is content is NOT empty


How can I amend this to refer to a CPT as well as a standard post?

Can someone point me in the right direction?

  • Have you tried this with a CPT? It should work with that code, CPT's and posts are the same, they just have a post type field that's different, but they're handled and stored the same way – Tom J Nowell Apr 26 '17 at 13:52
  • Yeah, I am using the same code via get_template_part() function. It works on pages but not on my CPT posts. I thought it may be down to the post type. – tmgale12 Apr 26 '17 at 13:54
  • 1
    I'd check to make sure there is a $post object at this point in the CPT. Maybe your queries are set up differently in the CPT than the default single.php. You could also try changing the conditional to if(empty($post->post_content)) in case there are blank spaces or something odd like that. – WebElaine Apr 26 '17 at 15:36
  • agreed, really these kinds of checks should be inside the main post loop – Tom J Nowell Apr 26 '17 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.