How to Display Properly Formatted Content From a $post Object in WordPress

Occasionally, I’ll run into a situation where I need to call a post specifically using the get_post() function.  get_post() returns a Post object, with a number of member variables – the one that stores the actual content of the post being post_content.

However, if you try just echoing $post->post_content, you’ll get completely unformatted text – which is frustrating, because WordPress uses a great visual text editor on the backend – it’s a shame to not take advantage of it.  Fortunately, WordPress provides a filter to display the text just as it would be displayed in the the_content() function inside the loop.  Here’s how it works:

To get the content formatted properly, we need to apply a filter (which is different from adding a filter – here, we’re registering it so that other filter functions can modify it before it’s displayed).


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

So, we’re just applying the the_content filter to the $post->post_content variable.  Doing so means that any plugin which adds a filter function to the_content like this:


add_filter('the_content', 'my_content_manipulator');

function my_content_manipulator($content){

//do stuff here

return $content;

}

will be applied to your variable.  Fortunately, it also means that the WordPress core functions which format the post content correctly can also work on it.

8 Comments »

8 Comments

  1. You could also use setup_postdata on the post object itself and then use the built-in display functions. Depends on your use case scenario, though.

    BTW, the fact that I can’t tab from your comment reply textarea to the name (even though the name input comes after the textarea) is somewhat frustrating. Might want to fix the tab order.

  2. I’ve always been a little fuzzy on how (and why) setup_postdata() works. I’ll have to look into it. Either way, thanks for the tip.

    And the tab order is fixed now – I had it set up for all you dyslexic tabbers out there, who like tab to work exactly the opposite of how they expected – but I’ve had to give into the vocal minority :)

  3. HELP!!!

    How would I add the filters to this code. The following is from the Plugin List Category Posts. Its displaying the content but without line breaks. I want to add a filter and have tried everything.

    if($atts['content']==’yes’ && $single->post_content){
    $output .=”$single->post_content”;
    }

    This is the specific web address http://easymoney4students.com/focus-groups/miami/ using the plugin.

    The page is set to display the following line.

    [catlist id=19 catlist content=yes numberposts=5]

    • Sade, have you tried this:

      if($atts['content'] == 'yes' && $single->post_content){
      $output .= apply_filters('the_content', $single->post_content);
      }

  4. it worked!!!! :) Thanks a million!

  5. Thanks a lot!

    I also had a problem getting “List category posts” output filtered/formatted output on my front page.

  6. Peter,

    Thanks so much! I’m just learning to integrate WP blogs tightly into a site, and will perhaps use it as a CMS although I’m just about to learn that part. I want full-on control of what I code.

    Anyway, I normally would write like this:

    echo ” . the_content.” . “\n”

    but then found the the_content() function was already echoing the content, and thus, I’d get doubles. I was frustrated that I was going to have to change my writing/coding style to accommodate the “poorly” written wp functions (I almost think a function should NEVER echo unless the developer tells it to, via, um, echo).

    But I have found that you (and wp) are both smarter than me, as they allow quick outputting of stuff, or accessing the post object with a filter. VERY cool and exactly the fix I was looking for. Thank you!!!

  7. Oops. Some single quotes got stripped… oh well.

Leave a Reply