Like the other Jetpack modules, the Related Posts module includes filters allowing you to customize the look of the Related Posts to fit all your needs.
The code snippets below provide examples of some of the filters included in the module. You can add these code snippets to a functionality plugin, or to your theme’s functions.php file.
You can also check Jetpack’s source code to discover more filters.
- Change the number of Related Posts displayed under your posts
- Move the Related Posts to the top of your post content
- Replace one of the Related Posts by a custom result, for a specific post
- Exclude a specific post from ever appearing among Related Posts results
Change the number of Related Posts displayed under your posts
By default, The Related Posts section will include at most 3 posts. You can change this number thanks to the jetpack_relatedposts_filter_options
filter, like so:
function jetpackme_more_related_posts( $options ) { $options['size'] = 6; return $options; } add_filter( 'jetpack_relatedposts_filter_options', 'jetpackme_more_related_posts' );
Move the Related Posts to the top of your post content
By default, The Related Posts section will show up at the end of your post. You can change it to show above the post content thanks to the jetpack_relatedposts_filter_options
filter, like so:
function jetpackme_move_related_posts_to_top( $options ) { $options['show_above_content'] = true; return $options; } add_filter( 'jetpack_relatedposts_filter_options', 'jetpackme_move_related_posts_to_top' );
Replace one of the Related Posts by a custom result, for a specific post
In the following example we want to append post ID 1036
as the first related post result so we will just filter the returned ‘hits’ array:
function jetpackme_append_related_post( $hits, $post_id ) { // $post_id is the post we are currently getting related posts for // Add 1036 to the front of the hits array array_unshift( $hits, array( 'id' => 1036 ) ); return $hits; } add_filter( 'jetpack_relatedposts_filter_hits', 'jetpackme_append_related_post' );
Exclude a specific post from ever appearing among Related Posts results
In the following example, 1037
and 1038
are the IDs of the posts we want to exclude:
function jetpackme_exclude_related_post( $exclude_post_ids, $post_id ) { // $post_id is the post we are currently getting related posts for $exclude_post_ids[] = 1037; // Exclude post_id 1037 $exclude_post_ids[] = 1038; // Also exclude post_id 1038 return $exclude_post_ids; } add_filter( 'jetpack_relatedposts_filter_exclude_post_ids', 'jetpackme_exclude_related_post' );
If you have more questions about the Related Posts module, do not hesitate to send us an email!
