Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Related Posts by Taxonomy Plugin

Posted on May 16, 2011 by Pippin in Free, Plugins, Utilities 30 Comments
Home» Plugins » Free » Related Posts by Taxonomy Plugin
Tweet
Love It - 1

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
Tweet Follow @pippinsplugins
post types, Related posts, taxonomies

30 comments on “Related Posts by Taxonomy Plugin”

  1. Bowe says:
    May 26, 2011 at 10:41 am

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

    Reply
    • pippin says:
      May 26, 2011 at 3:11 pm

      Yes, exactly.

  2. Nova Stevenson says:
    June 13, 2011 at 3:38 pm

    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

    Reply
    • pippin says:
      June 14, 2011 at 1:59 am

      @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 says:
    June 14, 2011 at 7:25 am

    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

    Reply
  4. Nova Stevenson says:
    June 14, 2011 at 7:25 am

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

    Reply
    • pippin says:
      August 2, 2011 at 3:16 pm

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

  5. Write a Better Related Posts Plugin for Custom Taxonomies | Pippin's Plugins says:
    September 14, 2011 at 9:06 pm

    [...] coding isn’t quite your thing, you can download the complete plugin for free here. Tweet Follow [...]

    Reply
  6. cfabrice says:
    January 28, 2012 at 7:46 am

    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

    Reply
    • Pippin says:
      January 30, 2012 at 9:09 am

      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"
  7. Mike says:
    April 1, 2012 at 10:22 pm

    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.

    Reply
    • Pippin says:
      April 1, 2012 at 11:28 pm

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

  8. lupetalo says:
    April 4, 2012 at 9:31 am

    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…

    Reply
    • lupetalo says:
      April 4, 2012 at 9:45 am

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

    • Pippin says:
      April 4, 2012 at 2:46 pm

      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 says:
      April 4, 2012 at 2:57 pm

      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 says:
      April 4, 2012 at 3:32 pm

      Sorry, I don’t understand what you mean.

    • lupetalo says:
      April 4, 2012 at 3:44 pm

      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 says:
      April 4, 2012 at 4:58 pm

      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 says:
      April 4, 2012 at 5:02 pm

      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 says:
      April 4, 2012 at 5:49 pm

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

    • lupetalo says:
      April 5, 2012 at 2:31 am

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

    • Pippin says:
      April 5, 2012 at 10:02 am

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

    • lupetalo says:
      April 5, 2012 at 10:41 am

      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…

  9. Mike Hemberger says:
    February 27, 2013 at 2:36 pm

    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

    Reply
    • Pippin says:
      March 2, 2013 at 4:38 pm

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

    • Mike Hemberger says:
      March 4, 2013 at 2:55 pm

      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.

  10. Chris Raymond says:
    March 21, 2013 at 1:33 pm

    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?

    Reply
    • Pippin says:
      March 22, 2013 at 4:30 pm

      Can you show me your complete code?

  11. Chris Raymond says:
    April 29, 2013 at 9:54 am

    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!

    Reply

Leave a Reply Cancel reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

  • Password Protect Taxonomy Terms and All Posts in the Terms
  • ECPT: Filter by Taxonomy Add-on
  • Filter Posts by Custom Taxonomy in Admin
  • Review: nrelate Related Content WordPress Plugin
  • Advanced WordPress Widget Bundle

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes

Latest Tutorials

  • Storing Session Data in WordPress without $_SESSION (19)

    The term Session in web development refers to...

  • Test Your Plugins with RTL (1)

    Right-To-Left languages are those that...

  • Submitting Your First Pull Request to a WordPress Plugin on Github (5)

    Github is an extremely popular tool for managing WordPress plugins, and one...

Enter your email to receive automated updates when new posts are published

WP Core Contributions

  • [24316]

View the ticket on Trac.

WP Codex Contributions

  • Function: shortcode exists
  • Function: has shortcode
  • Function: shortcode exists
  • Function: shortcode exists
  • Function: has shortcode

View all 41 changes in the Codex.

Latest Tweets

  • Could not fetch Twitter RSS feed.

Topics

Tom McFarlin meta box register_setting add_shortcode attachments wp_enqueue_script Rémi Corson Sugar Event Calendar the_content shortcodes add_options_page get_user_meta contextual help Related posts image plugin forms do_action login mail chimp authors short codes attachment comments recent posts apply_filters post types taxonomies short code bbpress custom post type Ajax images gallery Stripe taxonomy jquery widgets users add_filter add_action easy content types widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2013 Pippin's Plugins