Support » Fixing WordPress » How to insert a list of all posts with a particular tag within a post?

  • Resolved High Entropy


    I’m trying to figure out how to insert a list of additional posts within a blog post, but I’m drowning in PHP code and widgets! Here’s what I’m looking for:

    • I need a list of posts with a particular tag to be returned, and I’d like all posts to be listed, not just 5 or 10. The list should consist of clickable post title links.
    • I need this list to be embedded in the post itself, not in a sidebar. (And it’s posts I’m using, not pages.)
    • What I need is similar to the ‘Related Posts’ thing that I’ve seen on some blogs, but the list I want will probably NOT be related to the tag of the post currently being displayed. (To clarify, I don’t want a list of posts with the same tag as the one users are currently reading. The tag would be different, and I want to be able to hand-code each tag specific to each post).

    This seems like it shouldn’t be too complicated, but I only have an extremely basic understanding of PHP and I can’t find anything that does quite what I want. I’ve also looked at various plugins, but nothing is quite right. I could easily create a link that would open a search result with all posts with a particular tag, but it’s how to embed the results in the post itself?

    I have installed a plugin which allows me to insert PHP snippets into my posts, so I was wondering if that might be the best way forward? I’m going to have at least 50 pages that I’ll want to use this code on and most of them will need to show different lists. The site is being developed locally at the moment and isn’t online yet.

    I’m hoping this is simple and that I’ve just fried my brain with information overload! I’d appreciate any help anyone can offer. Thank you.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator keesiemeijer



    Try it with a shortcode like this in your theme’s functions.php:

    function tag_posts( $atts ) {
      $output = '';
    	extract( shortcode_atts( array(
    		'slug' => '',
    	), $atts ) );
    	if($slug != '') {
    		$tag_posts =  get_posts('posts_per_page=-1&tag='.$slug);
    		if($tag_posts) {
    			foreach($tag_posts as $tag_post){
    				$output .= '<a href="' . get_permalink($tag_post->ID) . '">' . $tag_post->post_title . '</a><br/>';
      return $output;
    add_shortcode( 'tag', 'tag_posts' );

    With this you can insert the shortcode in to your post content like this:

    [tag slug="my-cool-tag"]

    Use the tag slug of the tag which post titles you want to insert in the post content.

    Hi keesiemeijer, thanks for your speedy response!

    Your code works perfectly and I’m getting back a list of the posts I want and the code will certainly be simple to implement across many posts. Thank you for that!

    There’s just one small issue. It seems that no matter where I put the shortcode within the post, the list is being returned at the top before the other content. Is there any way to move the list down to the bottom of the page? I created a new post with only a couple of paragraphs of regular text (just in case something else was interfering) with the shortcode at the bottom, but the list always comes back first above the other content.

    Just FYI, I’m using a child theme of the 2012 theme, though my customisation have been CSS only. There’s nothing else in the functions file. I tried changing to the 2011 theme and adding the code to that functions file, in case it was an issue related to the 2012 theme or my customisations, but it does the same thing in 2011 too.

    Is there any way to move the list to the bottom of the post? Thank you again for taking the time to help me with this. It’s much appreciated.

    Moderator keesiemeijer



    That’s strange. On my site the links show up at the place where I inserted the shortcode. Can you share the link to your site?

    Unfortunately the site I’m working on isn’t on the internet yet. However, I’m working off and on with another WordPress site for a family member, so I quickly tried it out there, and it’s doing the same thing.

    You can see it here:

    The shortcode has been placed at the very bottom under the two paragraphs of text, but it’s returned above the other post content.

    Any ideas?

    Moderator keesiemeijer



    I made some edits to the code after I posted it. Are you using the same code (check above)?

    It works!! I must have copied the older code.

    What I have now is absolutely perfect! Thank you SO much! I really appreciate you taking the time to help me with this. 🙂

    Moderator keesiemeijer



    You’re welcome. I’m glad you got it resolved 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to insert a list of all posts with a particular tag within a post?’ is closed to new replies.