I'm trying to pull raw post content in order to identify if a particular shortcode is used, and if so, what its properties are. All of the properties of my $post object return what I expect except for post_content, which seems to give me an empty value. I've tried removing all markup from the body in order to avoid any potential pre-processing, but even then I get nothing. Here's how I try for the content;

    $content_post = get_post($post->ID, OBJECT, 'edit');
    $content = $content_post->post_content;

I got the same result using

    $content_post = get_post($post->ID, OBJECT, 'raw');

Similarly trying to pull the body with

$post_content = get_the_content("", false, $post->ID);

Got me nothing.

I did also try

$post_content =  apply_filters('the_content', $post->post_content);

Which got me back an empty span;

<span class="cp-load-after-post"></span>

This being my first WP project, I'm hoping I missed something simple...

Update: I do see the expected text in the post_content field in the zl_posts table.

  • 1
    Are you sure your posts contain content? A page builder could be storing it in post meta or another non-standard place. Also have you checked developer.wordpress.org/reference/functions/get_the_content ? If you can call $post->ID then why do you need to retrieve it with get_post? Couldn't you just use $post->post_content and avoid the unnecessary variable and get_post call? Most people using get_post only pass the first parameter – Tom J Nowell Sep 2 '20 at 16:00
  • How would I check for post meta? I called get_post because I was in someone else's code, hoping the $post object I had was not the one I expected, causing my missing content. Just tried get_the_content and one other, updated Q to reflect that. – GoetzOnline Sep 2 '20 at 16:18
  • Can you expand your code snippet to include all the surrounding code? What you've shared is not enough to debug this. Also, why are you trying to look for a shortcode and its properties? What's the problem that solves? ( and why didn't you ask about that problem instead? ) – Tom J Nowell Sep 2 '20 at 20:28
  • If you want to get post meta, you get_post_meta, WP isn't an object oriented application, and WP_Post is just a data bag, you should use the APIs at every opportunity rather than the various objects. If a function exists for it, use the function not the member variable. For that reason, it makes no sense to call get_post again as you already have a post object, it'll likely return the exact same object too. To answer this question though, you must add more code, the snippet you've shared is not enough. – Tom J Nowell Sep 3 '20 at 10: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.