Support » Plugin: Related Posts by Taxonomy » How to add a class to current post in widget

  • Resolved dominmax


    Is there a way to add a class to the current post title in widget? E.g. something like before/after_title.
    I added the code (from your other support post) that shows current post but I would like to make it bold.

    add_filter( 'related_posts_by_taxonomy', 'my_theme_related_posts_add_current', 10, 4 );
    function my_theme_related_posts_add_current( $results, $post_id, $taxonomies, $args ) {
    	// Check if it's related posts for a widget or shortcode.
    	if ( ! in_array( $args['type'], array( 'widget', 'shortcode' ) ) ) {
    		return $results;
    	// Check if fields is empty (not used by widget or shortcode).
    	if ( $args['fields'] ) {
    		return $results;
    	// Get the current post.
    	$curr_post = get_post( $post_id );
    	// Check if current post and related posts are found. 
    	if ( $curr_post && ! empty( $results ) ) {
    		// Add the relatedness score to the current post.
    		// Could be usefull if needed later in the process.
    		$count = count( $args['related_terms'] );
    		$curr_post->score = array( $count, 0 );
    		$curr_post->termcount = $count;
    		// Add current post to the related posts.
    		array_unshift( $results, $curr_post );
    	return $results;

    By the way. Using your plugin I made something like menu – I put a few Related Post by Taxonomy widgets for different custom post types.
    And now I have current post link in every group of post (e.g. you can see “Barcelona”) in every group:

    Is this possible to check if the current post is from the same CPT? Then it would show the current link only with one group of related posts (with the same CPT).

    Thank you in advance for help.

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author keesiemeijer


    Hi dominmax

    Sorry for the late reply. I’m working on a new version where you can add classes to the related posts items and include the current post at the top (more easily). This isn’t the first time someone requested this. I will be adding a new (fully tested) development version in a few days so you can check it out. I will let you know in this topic when that is (and what steps to take).

    Thank you in advance for the coming update 🙂
    Your plugin is unique and really worth further developing.

    Best for you.

    Plugin Author keesiemeijer


    I’ve added the development version to the repo. You can download it here.

    Please remove the previous code you have in your functions.php file.

    To include the current post you can use the “include_self” parameter introduced in 2.3.2. Use this code in your (child) theme’s functions.php file to use this parameter for the widget.

    add_filter( 'related_posts_by_taxonomy_widget_args', 'rpbt_widget_include_self', 10, 2 );
    function rpbt_widget_include_self( $args, $instance ) {
        $args['include_self'] = true;
        return $args;

    To add a current class to the current post add this in your functions.php file.

    add_filter( 'related_posts_by_taxonomy_post_class', 'rpbt_add_current_class_included_post', 10, 4 );
    function rpbt_add_current_class_included_post( $classes, $post, $args, $index ) {
    	$post_id_exists = isset( $post->ID ) && isset( $args['post_id'] );
    	if ( $post_id_exists && ( (int) $post->ID === $args['post_id'] ) ) {
    		// Add class current
    		$classes[] = 'current';
    	return $classes;

    Is this possible to check if the current post is from the same CPT? Then it would show the current link only with one group of related posts (with the same CPT).

    Can you explain what it is you want a bit more.

    consider creating a child theme instead of editing your theme directly – if you upgrade the theme all your modifications will be lost. Or create a plugin with the code above.

    Thank you very much!
    My dream came true 🙂

    Here you can see this working on my site.

    I have the last question in this subject: Is this possible to keep the same order in the group of taxonomy with current post? I mean – to have the current post not at the top but at the position of the default order of the taxonomy group?

    I guess it is not so easy but maybe??

    Thanks again.

    Plugin Author keesiemeijer


    Can you download the development version again

    And change this in your functions.php file:

    $args['include_self'] = true;


    $args['include_self'] = 'regular_order';

    This orders the related posts in order of common terms (and date). It means that the included post doesn’t show up if there are more recent posts (with the same terms) than the posts_per_page setting.

    Plugin Author keesiemeijer


    I’m also asking myself if you really need the related posts ordered by terms in common because you use it 6 times in a page. A normal query for posts with the the same taxonomy terms (ordered by date) is much cheaper than a query for related posts.

    It’s working 🙂 Thank you again.

    I could code the normal query for posts with the same taxonomy terms but then I should group the posts in the custom posts groups to make the menu ordered.
    And I am not sure (I don’t now) how to do this with one query (how to print the results of the query grouped with custom types). I can only do this separately for every cpt.
    I am 80% designer and only 20% programmer 😉
    That is why I am using your great plugin.

    Best for you

    Plugin Author keesiemeijer


    You’re welcome 🙂

    Just to give you an idea, you could use this filter to query for your own posts and still use this plugin:


    add_filter( 'related_posts_by_taxonomy_pre_related_posts', 'rpbt_highjack_related_posts', 10, 2 );
    function rpbt_highjack_related_posts( $related_posts, $args ) {
    	$post_id    = isset( $args['post_id'] ) ? $args['post_id'] : '';
    	$post_type  = get_post_type( $post_id );
    	$post_types = km_rpbt_get_post_types( $args['post_types'] );
    	$taxonomies = km_rpbt_get_taxonomies( $args['taxonomies'] );
    	if ( ! ( $post_id && $taxonomies && $post_type && $post_types ) ) {
    		return array();
    	// Get the terms for the current post.
    	$terms = wp_get_object_terms( $post_id, $taxonomies, array( 'fields' => 'ids' ) );
    	if ( ! $terms ) {
    	// Create the tax query
    	$tax_query = array();
    	foreach ( $taxonomies as $taxonomy ) {
    		$tax_query[] = array(
    			'taxonomy' => $taxonomy,
    			'field'    => 'term_id',
    			'terms'    => $terms,
    	$tax_query['relation'] = 'OR';
    	$query_args = array(
    		'post_type'      => $post_type, // Or use $post_types for all post types selected in the widget
    		'tax_query'      => $tax_query,
    		'posts_per_page' => $args['posts_per_page'],
    	// Get posts with same terms
    	$related_posts = get_posts( $query_args );
    	return $related_posts;
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to add a class to current post in widget’ is closed to new replies.