A site I’ve been working on had a list of “recent posts” displaying underneath the main content. The feature used a WP_Query loop to display an image, title, and author link for each recent post.
The client wanted to update this to use a “related posts” algorithm instead, but didn’t want to change anything else about the design or display. That got me curious if it would be possible to fetch the IDs from the Related Posts module in JetPack, but then run them through WP_Query to output the existing custom markup.
Related Posts with Fallback
Here’s the code I came up with. If the JetPack Related Posts module is active and results are returned, it builds an array of post IDs in $related_posts and queries for those. If not, the query returns recent posts.
<?php
$related_posts = array();
$query = array();
// Number of posts to show
$query['showposts'] = 3;
// Fetches related post IDs if JetPack Related Posts is active
if ( class_exists( 'Jetpack_RelatedPosts' ) && method_exists( 'Jetpack_RelatedPosts', 'init_raw' ) ) :
$related = Jetpack_RelatedPosts::init_raw()
->set_query_name( 'theme-custom' ) // optional, name can be anything
->get_for_post_id( get_the_ID(), array( 'size' => $query['showposts'] )
);
if ( $related ) :
foreach ( $related as $result ) :
$related_posts[] = $result[ 'id' ];
endforeach;
endif;
endif;
// Sets query to related posts, falls back to recent posts
if ( $related_posts ) {
$query['post__in'] = $related_posts;
$query['orderby'] = 'post__in';
$title = __( 'Related Posts', 'prefix' );
} else {
$query['post__not_in'] = array( $post->ID );
$title = __( 'Recent Posts', 'prefix' );
}
?>
<div class="related-posts">
<h3><?php esc_attr_e( $title ); ?></h3>
<?php $related = new WP_Query( $query ); ?>
<?php while ( $related->have_posts() ) : $related->the_post(); ?>
<div class="related-post clearfix">
<div class="related-image">
<a href="<?php the_permalink();?>"><?php the_post_thumbnail( 'archive' ); ?></a>
</div>
<div class="related-content">
<h3><a href="<?php the_permalink();?>"><?php the_title( ); ?></a></h3>
<p>by <a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ) ); ?>"><?php the_author(); ?></a></p>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
Prevent JetPack from Displaying Related Posts
To prevent JetPack from automatically inserting its own related posts markup, you’ll need to use this filter:
<?php
/**
* Prevents Related Posts from displaying in content area
*/
function prefix_remove_rp() {
if ( class_exists( 'Jetpack_RelatedPosts' ) ) {
$jprp = Jetpack_RelatedPosts::init();
$callback = array( $jprp, 'filter_add_target_to_dom' );
remove_filter( 'the_content', $callback, 40 );
}
}
add_filter( 'wp', 'prefix_remove_rp', 20 );
If you want to prevent JetPack from outputting the stylesheets and JS for Related Posts, you’ll need to use a few more filters. JetPack concatenates this all by default, so you’d be losing the concatenation if want to strip out specific scripts and styles. If your server is running HTTP2, that would definitely be a performance boost- but otherwise I’m not sure if the trade off between HTTP requests and page weight would necessarily be worth it.