Include a Post within a Post or Page

Nesting: one inside another

I’ve seen this question come up quite a few times:

How do I put a post into another post?

How do I include a post inside a page?

I always thought it was a really rookie question by people who didn’t understand WordPress and how it works. And I suppose that may still be true to a certain extent, but a couple of occasions have lent themselves to using this for legitimate needs.

For example, for one of my clients, they wanted to return their equivalent product if a user searched for a competitor’s product on their site. They wanted to return the usual product information, but wanted information at the top of the page explaining that the product shown below is the equivalent product for what they searched for and to look for the specific product number in the product literature. So below that, it was necessary to paste the entire product information post which already exists. So this was a good reason to use this ability.

I did a bit of searching and found a solution from the good people over at Digging Into WordPress. It’s another snippet, so get your functionality plugin ready:

In short, what this snippet does is create a shortcode (include_post) in the last line, which has a single argument; the post ID (id, from line 2). The shortcode uses that post ID to find the appropriate post content and then returns that content in place of the shortcode.

The syntax for using the shortcode with the snippet above would be:

This will return the post content from post 3012 to wherever you paste it. That could be a post, page, a custom post type, or even a sidebar widget.

If you’re not sure how to get the post ID, go to your list of posts. When you edit one, the URL will include the post ID. In this example, the URL would look like this:

(Alternatively, you could add a column to your posts editor which shows the post ID next to each post.)

You could also use this functionality to create snippets of text that you use repeatedly and paste them into posts, or to paste common suggestions or disclaimers into posts, by creating posts specifically for those pieces of text and then using the shortcode to pull them into any other post. What have you used this for? I’m always interested to hear how you use these snippets.

27 thoughts on “Include a Post within a Post or Page”

  1. Brian says:

    I actually was looking for this solution last week. Looks like I’ll go back now and fix the website where I needed this. More precisely, I’d like to list excerpts of posts on a page, complete with pics. But I’ll work on this snippet you’ve included here first.

    1. Well that’s actually a very simple change. Instead of get_the_content in line 6, use get_the_excerpt instead. You’ll begin to see how you can use this tweak to get all kinds of information about the post, by using different functions (like get_the_title or get_the_post_thumbnail) and you can combine multiple components together (for example showing the post thumbnail alongside the content). Good luck!

  2. Richard says:

    This would be really useful when you have snippets of text, such as a product review or recommendation that you use multiple times. It’d be much easier just to create a post once and then include that post whenever you need the snippet.

  3. Hi Dave, I was looking to post within a post for linking and advertisement purposes for a client and since I am new,I have not been able to crack it, been finding this code for weeks! Thanks very much.

  4. Frank Bowes says:

    Hey Dave, I will definitely be able to use this on my WP blogs, but does it work for Blogger as well? I have a site on Blogger and this may be useful for working with their sub-standard page options.

    1. I’m fairly confident that it won’t work on Blogger because the hooks are unique to WordPress. I’m not even sure if you can create functions like you can with WordPress as I don’t have much experience with it (for good reason). I’d just recommend going with WordPress for all of your sites ;)

  5. Rodney Beck says:

    I was looking for something like this and was bummed when it wouldn’t work in WordPress v3.3.1. I don’t know if it was WordPress or my website but it wasn’t returning the post at all. I rewrote the function and now it work fine and the bonus is it doesn’t touch the main loop.

    function diww_include_post($atts) {
    $thepostid = intval($atts[id]);
    $output = ”;
    $post = get_post($thepostid);
    if ($post) :
    $output .= apply_filters(‘the_content’, $post->post_content);
    else :
    // failed, output nothing
    endif;

    return $output;
    }

    1. Well, I’m using 3.3.1 and it’s working for me. Must be some kind of conflict specific to your site, rather than WordPress in general.

      This is another way of doing it, and I thank you for pasting this, however, the original snippet doesn’t modify the main loop; it uses a separate query to pull in the post to be included.

      1. Rodney Beck says:

        Fair enough. Could be the theme I’m using or some other random thing.

        Sorry I jumped the gun on the main loop thing. Didn’t take much notice of the wp_reset_query() call there.

  6. Marlice says:

    Hello,
    nice function – thank you for sharing! I tried it on my blog and it works very fine with posts. But I could not figure out how to get a page or a widget in a post. Do I have to extend your function for this?
    Greetings from Germany,
    Marlice

    1. Widgets would be an entirely different story, but pages should be the same process. Just get the post ID and include that in the shortcode.

  7. DavidG says:

    Awesome stuff mate- thanks for this. One quick thing. Say I wanted to call the post thumbnail in as well, could I achieve this with this? I assumed I’d have to add the post_thumbnail bit in before content. But not sure how it’d work with your function or as to the formatting etc

    1. Yeah, you can just include get_the_post_thumbnail into the $output variable to include it.

  8. thiloroe says:

    Hey Dave,
    this is just what I’ve been looking for – works great. With one exception: shortcodes that are in the post that is displayed in another post don’t get executed. A little shortcode within a shortcode problem, I guess. Any idea how to fix this?

    Thilo

    1. Yes, you should be able to solve that by changing line 6 to:

      $output .= do_shortcode( get_the_content($post->ID) );

  9. Amy Lansky says:

    Hi Dave. I’m trying to use your include_post but the sub-post has all the paragraph breaks missing…. Any ideas?

    1. Hmmm, you may have to apply the_content filter to the output, but I believe that get_the_content already does that, so I’m not sure why it might be stripping paragraphs.

  10. Hello,

    I’ve used Dave solution in one website and worked fine, but at another one it’s not working.
    So, I tried Rodney solution, and its working. But, I would like to show thumbnails WITH POST LINKS;

    //shortcodes inside posts
    function diww_include_post($atts) {
    $thepostid = intval($atts[id]);
    $output = ”;
    $post = get_post($thepostid);
    if ($post) :
    $output .= get_the_post_thumbnail($post->ID,’large’);
    else :
    // failed, output nothing
    endif;

    return $output;
    }

    add_shortcode(“include_post”, “diww_include_post”);

    I’m new at php, some one could help me?
    Thank you.

    1. Bibi, do you mean that you want the thumbnail to be linked to the post?

    1. In that case, something like this should do the trick

  11. Hi Dave,

    Thank you for this! It has been super helpful for my current project. Quick question: Do you know if it’s possible to include custom fields in the output? I’m playing around with some options and not having much luck yet.

    1. Hi Danielle, this is definitely possible. You’d need to add more content to the $output variable using the get_post_meta() function, similar in the way that the get_the_content() was used to get the post content.

  12. Tom K says:

    Hi Dave, great post. I have tried and failed to change it so that the text it pulls in from the other post comes in formatted as it was in the original. e,g. three paragraphs in the original post would come in and be displayed as three paragraphs in the new post. If it could drag in all the styling that would be even better. I am hoping to use this in the situation where I refer off to a particular service, organisation, description in multiple posts but only have to update the details in one location to avoid conflicting or out of date content. Any suggestions would be great.

    1. I’m not sure why it’s not coming through with the same formatting as the original post. You might try wrapping the content returned in wpautop(), something like this.

  13. Dan Schweitzer says:

    Dave this works great! So great that I would like to have a second post output on the same page. Is that possible?

    1. Sure, just use the shortcode twice in a row, e.g. [include_post id="3012"][include_post id="3013"]

Leave a Reply