Support » Plugin: Related Posts by Taxonomy » Count/Display common and total terms b/w two posts

  • Resolved Bethzy Elonia

    (@east2611)


    Hi keesiemeijer greetings 4m India, last time u helped me a lot.
    For eg: I have a custom post type ‘cars’ and taxonomy related to it ‘brands’ that has terms A , B , C , D , E.
    Now post one has A, C, D, E terms attached to it.
    And post two has A, C, D attached to it.
    There are 3 terms common in both posts i.e A, C, D.
    In related posts I want to display number of common terms/ total terms for post one i.e 3/4 and display names of those common terms i.e A, C, D.

    Final result should look like this:
    Post one title – 3/4 brands Common – A, C, D.

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

    (@keesiemeijer)

    Hi east2611.

    Can you tell me what format (titles, thumbnail etc) you’re using. Do you want the terms (A,C,D) linked to their archive page?

    I m using this shortcode <?php echo do_shortcode(‘[related_posts_by_tax taxonomies=”brands” post_types=”cars” posts_per_page=”5″ title=”More cars On Similar brands”]’);?> on single-custom-post-type.php i.e single-cars.php below post content. U can see this page http://guitarutha.com/kabira-guitar-chords/ post titles are shown below post content i want to display counts and terms next to post titles shown by ur plugin. I don’t want to link it to archives .

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Try it with this in you (child) theme’s functions.php file:

    
    add_filter( 'related_posts_by_taxonomy', 'rpbt_add_common_terms', 10, 4 );
    
    function rpbt_add_common_terms( $related, $post_id, $taxonomies, $args ) {
    
    	if ( !( isset( $args['related_terms'] ) && $args['related_terms'] ) ) {
    		return $related;
    	}
    
    	// get the terms (objects) used to calculate the related posts
    	$related_terms = get_terms( array(
    			'taxonomy' => $taxonomies,
    			'include'  => $args['related_terms'],
    		) );
    
    	// get the term names
    	$names = wp_list_pluck( $related_terms, 'name', 'term_id' );
    
    	foreach ( $related as $key => $post ) {
    
    		// Get the terms from the related post.
    		$terms = wp_get_object_terms( $post->ID, $taxonomies, array( 'fields' => 'ids' ) );
    
    		// Get common terms from the related post as array keys.
    		$in_common = array_flip( array_intersect( $terms, $args['related_terms'] ) );
    
    		// Get the term names.
    		$in_common = array_intersect_key( $names, $in_common );
    
    		$title = $post->post_title . ' – ' . count( $in_common ) . '/' . count( $args['related_terms'] );
    		$title .= ' brands Common  – ' . implode( ', ', $in_common );
    
    		$related[$key]->post_title = $title;
    	}
    
    	return $related;
    }

    btw:
    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.

    i m already using child theme and site-specific plugin. You are fabulous it worked like a charm, but there’s a lil problem with total terms. total terms are showing the numbers of terms of the post on which visitor is currently viewing, i want to show the total terms of the post, next to which we are dispaying this common terms.
    like:
    post title-0 on the top
    .
    .
    .
    .
    some post content
    .
    .
    .
    .
    more cars on similar brands(shown by ur plugin)
    1> post title-1 – 3/(show total terms of this post) – A,C,D
    2> POST TITLE-2 – 4/(show total terms of this post) – A,B,C,D.

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Try changing this

    
    $title = $post->post_title . ' – ' . count( $in_common ) . '/' . count( $args['related_terms'] );

    To this

    
    $title = $post->post_title . ' – ' . count( $in_common ) . '/' . count( $terms );

    PERFECT! just one last request i want links in posts title only not the terms info to be linked. terms info should be in black. see here http://guitarutha.com/kabira-guitar-chords/ both post title and info is linked in blue. and please tell me how to mark this topic resolved.

    Plugin Author keesiemeijer

    (@keesiemeijer)

    change this:

    
    		$title = $post->post_title . ' – ' . count( $in_common ) . '/' . count( $terms );
    		$title .= ' brands Common  – ' . implode( ', ', $in_common );
    
    		$related[$key]->post_title = $title;

    to this

    
    		$title = count( $in_common ) . '/' . count( $terms );
    		$title .= ' brands Common  – ' . implode( ', ', $in_common );
    
    		$related[$key]->in_common = $title;

    For it to show up you have to copy and edit the plugin file related-posts-links.php.
    See the first method here: https://keesiemeijer.wordpress.com/related-posts-by-taxonomy/templates/

    Now change this in the (child theme’s) template file related-posts-links.php

    
    <li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr( get_the_title() ? get_the_title() : get_the_ID() ); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a></li>

    To this:

    
    <li>
    	<a href="<?php the_permalink() ?>" title="<?php echo esc_attr( get_the_title() ? get_the_title() : get_the_ID() ); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a>
    	<?php echo isset($post->in_common) ? ' – ' . $post->in_common : '';  ?>
    </li>

    yeah it did the work, thanks again for the continuous support, marking it as resolved.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Count/Display common and total terms b/w two posts’ is closed to new replies.