Troubleshooting & How-Tos 📡 🏷️ 🔍 Programming

Hide Related Posts by Post Format

I finally got around to re-customizing the related posts on my blog. Specifically: hiding related posts on asides and links.

Years ago I had this set up when the site was running WordPress and Jetpack. I’ve since moved it to Contextual Related Posts (in order to keep it local on my server), and later to ClassicPress.

Neither plugin has features to skip based on post formats, but you can use hooks, actions and filters to write your own.

Contextual Related Posts

The plugin has options to exclude displaying by post type (post, page, or media), by category, or on individual post IDs…but not by tag or post format. So I dug around in the docs a bit, looking for ways to filter CRP’s input or output.

The simplest approach I came up with was to turn off automatically adding it to any page/feed/archive type in the CRP plugin’s general tab, then manually add it to the post’s output when and only when I want it included.

Option 1: Modify Your Theme

The CRP admin file suggests putting <?php if ( function_exists( 'echo_crp' ) ) { echo_crp(); } ?> in your theme template where you want it to display. To make it only display under some conditions, you’ll extend the if statement like this:

<? php
	if (
		get_post_format() != 'aside'
		&& get_post_format() != 'link'
		&& function_exists( 'echo_crp' )
	) {
		echo_crp();
	}
 ?>

echo_crp() sends the plugin’s output to straight to the page being rendered.

get_post_format() is self-explanatory.

Option 2: Use a Plugin or Functions.php

I put a lot of my blog customizations in a local functionality plugin, so I can switch themes without losing my changes. (Admittedly, it’s been a long time since I last switched themes on my blog.) This should also work if you put it in a functions.php file in your theme.

The idea here is that you add a filter to the_content and decide whether or not to add the related posts lists to the end.

// Manually show Contextual Related Posts output, so we can skip displaying it on asides and linkposts
function exampleprefix_manual_crp($content) {
	if (
		is_single()
		&& get_post_format() != 'aside'
		&& get_post_format() != 'link'
		&& function_exists( 'get_crp' )
	) {
		$content .= get_crp();
	}
	return $content;
}
add_filter('the_content', 'exampleprefix_manual_crp', 999);

is_single() will ensure it’s only shown on individual post views, not in feeds or category pages or the home page.

get_crp() is similar to echo_crp(), but instead of pushing the output to the page, it returns the output so you can work with it.

999 is the placement of this filter, and a convenient number to make sure it’s all the way at the end.

Jetpack Related Posts

I forget where I found this since it was probably a decade ago. Maybe on the Jetpack support forum, maybe on StackExchange.

The approach I used was to check the post conditions, find Jetpack’s related posts filter, and then remove it from the active filters on the_content.

// Don't show Jetpack Related Posts on asides or links
function exampleprefix_hide_jetpack_related_on_asides() {
	if ( class_exists( 'Jetpack_RelatedPosts' )
		&& (get_post_format() == 'aside' || get_post_format() == 'link')
	) {
		$jprp = Jetpack_RelatedPosts::init();
		$callback = array( $jprp, 'filter_add_target_to_dom' );
		remove_filter( 'the_content', $callback, 40 );
	}
}
add_action( 'wp', 'exampleprefix_hide_jetpack_related_on_asides', 20 );

It turns out there’s a simpler way to do it now. (Maybe there was back at the time, and I just found the complicated one first.) The Jetpack Related Posts Developer Reference has an example where you just turn off the feature when it checks the options:

// Don't show Jetpack Related Posts on asides or links
function exampleprefix_hide_related_posts_jetpack( $options ) {
    if ( get_post_format() == 'aside' || get_post_format() == 'link' ) {
        $options['enabled'] = false;
    }
    return $options;
}
add_filter( 'jetpack_relatedposts_filter_options', 'exampleprefix_hide_related_posts_jetpack' );