WordPress Support and Resources

Plugins, Training, Sample Code and more for WordPress Websites

Plugins

Need to extend your WordPress site? Each of our plugins are high quality and serve a specific need within the WordPress community.

Get Plugins

Quizzes

Are you a WordPress expert? Try your hand at these WordPress quizzes to see how good your skills are.

View Quizzes

Support Forums

Stuck on an issue? Something on your mind? Post your question to get support from the community.

Support Forums

Latest Posts

Getting the Post Content Outside the 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);
Custom Taxonomies as a List of Links

Display a custom taxonomy list as links for a post

If you create a custom post types, chances are that you may also include creating custom taxonomies.  There may be times when you want to display these custom taxonomies in an isolated fashion away from other taxonomies.

This snippet of code allows you to pass in a POST_ID along with the keyname of a custom taxonomy.  The routine then queries the WordPress database to retrieve all taxonomies of the passed type that are assigned to the past post id.  The routine then continues by displaying the taxonomies in a nice clean list for visual display.

/**
 * @param $post_id int          The post ID to get the terms for
 * @param $taxonomy string      The taxonomy type to the get the terms for
 * @return string               The string ready for output with links and taxonomy terms
 */
function get_taxonomy_terms_as_links($post_id, $taxonomy){

    // Call for the list of taxonomies to be used
    $taxonomies = get_the_terms($post_id, $taxonomy);

    // Validate we have data
    if(!empty($taxonomies)){

        // Iterate through each taxonomy, create a link and add it to our list
        $taxonomy_list = array();
        foreach($taxonomies as $taxonomy){
            if(!empty($taxonomy->name)){
                $taxonomy_list[$taxonomy->name] = '<a href="'.get_term_link($taxonomy).'">'.$taxonomy->name.'</a>';
            }else{
                // Not a valid taxonomy object
            }
        }

        // Sort the terms alphabetically
        ksort($taxonomy_list);

        // Create a single string
        if(!empty($taxonomy_list)){
            return implode($taxonomy_list, ', ');
        }
    }

    // Return nothing if we are here
    return '';

}

// Example getting the built in tags for post id 350
echo get_taxonomy_terms_as_links(350, 'post_tag');