There’s a bunch of plugins to automatically generate related posts – YARPP is my personal favorite. But some people prefer to have more control over their related posts and manually select them. This can be set up pretty easily using the Posts 2 Posts plugin.
There’s three pieces of code we’ll add to our core functionality plugin 1:
- Register the connection. This adds the metaboxes for linking posts.
- Pre-Loop code. This adds the related posts information to the $wp_query (better performance)
- Display Related Posts.
Register the Connection
I’m creating this connection from the
post post type, to the
post post type. So when you’re editing a post, you can select other posts to connect it to. This actually creates two metaboxes: a
from metabox that you use to connect the current post to others, and a
to metabox that shows what posts have already been connected to this one. I’ve labeled the
from metabox “Related Articles” since this is what is displayed as related to the current post, and I labeled
to “All Connections”.
Once you add this code to your site, you should see metaboxes like this when editing a post:
As described in the Posts 2 Posts wiki, when dealing with archive pages its much better to add the connected posts to the $wp_query before you run the loop.
Display Related Posts
You can of course display the related posts however you’d like. For my specific implementation, I wanted to limit it to 5, display the post image at a specific size, float the category name on top of the post image, and then display the title below the image.
The important thing to note is that
$post->connected is an array of post objects, so use the
foreach() to loop through them and then display the post data however you’d like.
Integrating it with your theme
Once you’ve added this code to your core functionality plugin, you have to actually call it in your theme. Just place
be_related_posts_pre_loop() before you call the loop, and
be_related_posts() somewhere inside the loop. If you’re using Genesis, it has hooks throughout the theme, so you just need to add the following two lines to your theme’s functions.php file (alter the second line to change the positioning of the related posts):
I’d love to hear any thoughts you have about this, especially if you have experience with Posts 2 Posts.
- Core Functionality Plugin
While you could put all this code in your theme’s functions.php file, a core functionality plugin is better for things like this because if you change your theme in the future, you’ll want to easily move this functionality to your new theme. If it’s in a plugin, all you have to do is add two lines to your theme (call the pre-loop code and then the display code). If all the code is in your old theme, you have to dig through all your files, figuring out how you built your related posts section so you can duplicate it in your new theme. In my core functionality plugin, I created /lib/functions/related-posts.php so that all the code was on one location.