video-poster

Related Posts Plugins are very popular as a method for WordPress users to display related content at the end of their posts, providing the reader with additional relevant content. Typically, related posts plugins use Post Tags to compare posts and find those that are related to the currently displayed post. The problem with this method is that it doesn’t work for any post type that doesn’t use Post Tags or that has custom taxonomies.

This plugin will provide a function that will allow you to display 4 posts that are related to the currently viewed post. By default it will display posts that share similar Post Tags, but by passing a parameter to the function, you can also display posts that are related by a custom taxonomy.

To display the related posts, place this inside your single.php loop:

1
echo pippin_related_posts();

This will display an unordered list of four related posts.

If you want to display posts related by a custom taxonomy, such as “Genres”, then you can do this:

1
echo pippin_related_posts('genres');

You can see this plugin in action (in a customized form) on the My Works template available from Mojo Themes and written by your’s truly and AJ Clark of WP Explorer.

Download
  1. Bowe

    So you could use this to show different restaurants with the same taxonomy in a CPT right? If so.. awesome :-)

    • pippin

      Yes, exactly.

  2. Nova Stevenson

    I got this message when I activated the plugin

    “The plugin generated 4 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.”

    Should I be worried about this?

    Also where exactly should the bit of code go in the loop?

    cheers

    Nova

    • pippin

      @Nova – If it doesn’t appear to cause any problems, then you are fine.

      The code should be placed after the while have_posts() and before endwhile;

  3. Nova Stevenson

    Hi Pippin,

    Thanks I’ve got it working but it is brining back the post that it is related to in the results twice?

    http://inside-industry.org.uk/testing123456/careerpaths/geophysicist/ (similar career paths at the bottom of the page)

    Not sure if it is possible with this plugin but I would also like to display another post type which is related by a shared taxonomy. For example the link above shows your plugin pulling through other career paths related by subject but college courses share the subject taxonomy and I’d like to display college courses related to that career.

    Is there a way to specify the content type and the taxonomy?

    cheers

    Nova

  4. Nova Stevenson

    p.s I’m using your content types plugin for my custom post types and taxonomies if that helps

    • pippin

      @Nova – So sorry, I seem to have missed your comment. Did you get this fixed?

  5. cfabrice

    Hello,
    the plugin works great !
    but I would like to output the classes or at least the category class of my post because I’m using this class to style them.
    I’ve tried to modify the code at the bottom of your plugin, without much success using the post_class function, I’ve tried get_post_class as well
    the best I got is all the class of my posts displayed together above the lists of posts…
    and with get_post_class, I got li class=array all the time
    here is my code and some comments:
    http://snippi.com/s/rba1zpq
    any help ?
    Thank you

    • Pippin

      So get_post_class() always returns an array of class names, so you will need to separate them out into a string. You can do that like this:

      1
      2
      3
      
      $classes = array('class_one', 'class_two');
      $classes = implode(' ', $classes); // put classes into a space deliminated string
      echo $classes; // with output "class_one class_two"
  6. Mike

    Thanks a lot, this saved me a lot of time hacking it together myself., and probably yields a better result too. One thing I noticed, The plugin was breaking my WordPress back end when it reached pluggable.php because there appeared to be white space at the end of the plugin file. I got rid of it and things seemed to be just fine after that.

    • Pippin

      I’m glad you like it, and thanks for pointed out the extra white space.

  7. lupetalo

    After some time using it all pages in admin started to be white, no output. When i changed plugins file name everything worked, and when reactivating plugin again error:The plugin generated 4 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.
    It might be something else but i doubt.

    Also when cpt has more than one taxonomy selected and those have hierarchy it showing not only posts from same taxonomy but also from others.

    books
    -trilers
    –1980
    plugin shows not only cpt-s from 1980 but also from parent taxonomies…

    • lupetalo

      Deleteing those exta spaces fihed first part, but second is still here…

    • Pippin

      I’ve just updated the plugin to remove those extra lines. The second issue is actually a bug, or over sight, in the way that WordPress queries posts from taxonomies. When you have a term that is selected as the child of another, then the post filed in the child term is automatically considered to be filed in the parent term as well.

    • lupetalo

      Is there a way then to get list of cpt-s that are included in tahonomy that have parent but not include cpt-s from parent?

    • Pippin

      Sorry, I don’t understand what you mean.

    • lupetalo

      vhen displayung one cpt, i want to have a list of related cpt-s from same taxonomy displayes as list. But only from one taxonomy.
      books
      -trilers
      –1980

      let say when displaying book from tax 1980 list all from 1980 and not from trilers or books… and do that from single-cptname.php file…

    • Pippin

      In this case (without modifying the code) that’s not possible, simply because the posts that are pulled in are based on the terms of the current item. If your current item is filed into “1980”, then it is also considered (by WordPress) to be filed in “trilers” and “books”, so when related items are pulled from the database, those terms will be included.

    • lupetalo

      Any idea on how can i restrict showing only one tax, some type of if code… we know the name and id of tax from we want to show cpt so maybe we can filter out rest?

    • Pippin

      What it sounds like you want to do is manually specify the taxonomy term to pull other items from, correct?

    • lupetalo

      Yes that is exactly i need, can this be done with modification of plugin?
      Thanks for quick reply…

    • Pippin

      Yes, but I am unable to do it at this time.

    • lupetalo

      That would be nice addon for plugin since wp is like that. No rush, i solved problem in not that elegant way. Added metabox in cpt that defines rax that is being listed…

  8. Mike Hemberger

    I’m getting a fatal error when using this in my single.php template.

    Fatal error: Cannot use object of type WP_Error as array in /Users/JiveDig/Sites/newstiz.dev/wp-content/plugins/related-posts-by-taxonomy/related-posts-by-taxonomy.php on line 20

    • Pippin

      Sounds like it might not have retrieved any terms of the taxonomy. Does the post definitely have terms attached to it?

    • Mike Hemberger

      My bad Pippin… somehow when I was editing something else I removed the ‘echo’ from the code. Added it back and all is well now.

  9. Chris Raymond

    Hi Pippin, I put this in my single-project.php file, inside the endwhile, and it doesn’t generate any output. I also put it inside a div with ID of related, just to see what happens, and that markup gets generated but the plugin doesn’t seem to generate any output; the div is empty.

    My posts do have tags. I’m still in WP 3.2 due to compatibility with Gridnik theme. Any ideas? Is my tags not equivalent to a taxonomy in the current WP?

    • Pippin

      Can you show me your complete code?

  10. Chris Raymond

    Pippin, I actually resolved the above issue. I have a new question: How would I modify the plugin code to exclude the current post from showing in the Related Posts loop? It seems kind of odd to show a thumb and post title for the current post.

    thanks!

Your email address will not be published. Required fields are marked *