Get Post Content outside of WordPress Loop

Get content for a post not included in the loop

WordPress core focuses on the use of the content loop.  You start the page display with a query of posts to display, then you loop through each one and display that posts content.  there may however be circumstances in which you need to display the content of a post that is not included within the original query.  This code snippet below shows you how to accomplish this.

The key to why this routine is important is the handling of the get_the_content() function.  This routine allows you process the proper content hooks instead of just referencing the stored content value.  This is important because it will allow other plugins and routines to be executed against this content that is retrieved outside of the post loop.

It should be noted that this routine is not necessarily recommend as a best practice.  Whenever you add a routine that performs an out of band database call, you will increase latency to your site or plugin.  In some cases though, this may be the best option available for you.

In our sample below, we start by passing in a post id that we want to be displayed.  This post can be of any type including any custom post types that you may have created.  The routine then references the global $post variable and saves it into a temporary variable.  This is done because we need to temporarily use the global $post variable so that other WordPress routines get called correctly.

We retrieve our desired post from the database and call the setup_postdata() routine on our newly fetched post.  This routine sets up the global post variables so that we can use core WordPress routines.  With that setup, we can now call the_content() for the post we have retrieved and setup.

The last steps are very important.  In this case we have setup a random post in the global variable, so now we need to set it back.  We take our placeholder $original_post variable and set it back to the global $post variable.  Don’t forget to rerun the setup_postdata() on it to prepare it for global usage.

/**
 * Function used to get the content for a post ID while in the middle of a loop
 * Useful for shortcodes or other routines to get access to content from random posts
 *
 * @param $postId int       The post id to get the content for
 * @return string           The content for the desired post
 */
function get_content_for_post_id($post_id){

    // Need to use the global post object
    global $post;

    // Save the current post into a placeholder variable
    $original_post = $post;

    // the data to be returned
    $html = '';

    // Validate our post id is not empty
    if(!empty($post_id)){

        // Get the post for the passed ID
        $post = get_post($post_id);

        // Setup the post data for the newly fetched post
        setup_postdata($post);

        // Get the content for this post and run it through appropriate display filters
        $content = get_the_content();
        $content = apply_filters( 'the_content', $content );
        $html = str_replace( ']]>', ']]>', $content );

        // Set the original Post back to the global post object
        $post = $original_post;

        // Be sure the run the setup function on the original post so as to not mess it up
        setup_postdata($post);
    }

    return $html;

}

// Example getting the post content for post id 350
echo get_content_for_post_id(350);
Posted in and tagged .