Wordpress Related Topics Plugin

WordPress Related Topics Plugin

For a recent WordPress project I was working on I needed a way to display related taxonomies (topics) from a custom post type within a single taxonomy template. For this specific project, my custom post type was ‘video’ and its taxonomy was ‘topics’. For each taxonomy page I wanted to show a list of topics related to the video posts they had been tagged in.

Getting Started

First I needed to begin a new $wp_query with the settings below:

Next I want to pass these $args and find the relational terms for each post containing my term slug.

To view our progress at this point, a print_r($related) will show an array of topics and the number of times it has appeared. Success! But not quite there yet.

So next I need to do few things.

  1. I want to sort the output into a list and make it pretty.
  2. I only want to display 10 results.
  3. Out of these 10 results, I do not want to show the topic of the page I am currently on.
  4. If you did a print_r of the array, you’ll see that the output is not formatted for direct insertion into a URL, so I will need to remove the whitespace of this result and enter a hyphen for use again as a term slug. This is easily achieved with a str_replace and trim.

Finishing Up

Since this was something I wanted to show in a custom sidebar of my taxonomy template, I opted for creating a custom plugin. I didn’t need any options with the plugin, just a quick solution, so I just wrapped it up in a function to do what I needed.

Here is the final result:

Here is the final plugin version tested on WordPress 3.9.2. Since I didn’t need an options page, you’ll need to edit the file with your own custom post type and taxonomy to make it work for you.

Please feel free to change it to suit your needs or expand upon it.

Download WordPress Related Topics Plugin – 1.0

If you have any questions, comments, concerns (or criticisms) – leave a comment below.

Special thanks to M0R7IF3R over at WordPress Answers for his help pointing me in the right direction with this plugin.

Post updated on 8/19/2014

Author: Christi Richards Christi Richards is a freelance web developer and designer working out Youngstown, Ohio. She has over 16 years of working industry experience in both front and back end web technologies. Current and recent projects include NutritionFacts.org, The Jesse and Julie Rasch Foundation, and Hedgewood. You can follow Christi on Twitter, Facebook, or .