• Resolved Agrajag27

    (@agrajag27)


    I would love to see a feature where I can give you a list of tags and apply weights to them. Again, this is big for a site like ours with film reviews.

    Plot keywords, actors and directors are pretty much of equal weight for relating posts.

    Also important, but only about half as much, are genres. So a film where both star Tom Cruise and both involve Revenge is a great match.

    A lesser match would be a film that also matches by two tags, but those two tags are genre. Yeah, they’re both Action and Adventure, but that’s it.

    What I would do with this is go in and give you an entry for every genre (Action, Adventure, Drama, Mystery, Horror, etc.) and set them to say, .5 value. Thus, a film matching on two genres would only get essentially the equivalent of one match and would appear lower (if at all) over a relation on two tags both at default 1 weight.

    https://wordpress.org/plugins/related-posts-by-taxonomy/

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

    (@keesiemeijer)

    Hi again

    This is a very specific use case that can be done with the filters provided by this plugin.
    The related posts get a score of how many terms they have in common.

    If I understand correctly
    keywords, actors and directors and genre are taxonomies.
    Related posts with genre terms assigned should have a lesser score, or do all posts have the genre terms assigned?

    https://keesiemeijer.wordpress.com/related-posts-by-taxonomy/filters/

    Thread Starter Agrajag27

    (@agrajag27)

    Currently the different terms are all just Tags.

    We could move them to custom taxonomies if that’s the best way to handle this. I was thinking of a feature to just have them all weighted as Tags.

    So, here’s a typical real example:

    ——————————————-
    Post Title: Star Wars: The Force Awakens
    Tags: Movie Review, Disney, Sci-fi, Oscar Isaac, Adam Driver, Harrison Ford, J.J. Abrams, Adventure, Action, Fantasy, Space, Good vs Evil, Star Wars, Sequel, George Lucas, John Williams, John Boyega, Daisy Ridley, Peter Mayhew
    ——————————————-

    In this case the genre tags are Adventure, Action, Fantasy and Space. With this feature I’d put them in in one bucket and weight them .5.

    Thus, two of those tags would equal 1 of any other tag.

    FYI: I’d be willing to discuss custom work if you’re okay with calling me to go over the issues. I’m in the US EST. You can reach me at rich at slashcomment (.com)

    Plugin Author keesiemeijer

    (@keesiemeijer)

    I understand, let me see what I can do.

    Is this for the widget only? Or should this apply to the shortcode also?

    Thread Starter Agrajag27

    (@agrajag27)

    I would think others would want it for both. We only would use the widget.

    Thanks again for the consideration. At the very least is there a way to donate?

    Plugin Author keesiemeijer

    (@keesiemeijer)

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

    // return all related posts (with the widget) for re-ordering of posts by weight
    add_filter( 'related_posts_by_taxonomy_widget_args', 'rpbt_return_all_related_posts' );
    
    function rpbt_return_all_related_posts( $args ) {
    
    	// store posts_per_page in a temp variable
    	$args['_posts_per_page'] = $args['posts_per_page'];
    
    	// set posts_per_page to return all posts for re-ordering
    	$args['posts_per_page']  = -1;
    
    	return $args;
    }
    
    // re-orders related posts by weight
    add_filter( 'related_posts_by_taxonomy', 'rpbt_reorder_posts_by_weght', 10, 4 );
    
    function rpbt_reorder_posts_by_weght( $results, $post_id, $taxonomies, $args ) {
    
    	// Edit these genre terms ids to your genre term ids
    	$genre_term_ids = array( 12, 13, 40, 41, 56 );
    
    	// Check if it's related posts for the the widget
    	// The widget returns all related posts
    	if ( 'widget' !== $args['type'] ) {
    		return $results;
    	}
    
    	foreach ( $results as $key => $value ) {
    
    		if ( !isset( $value->related_terms ) || !isset( $value->score ) ) {
    			continue;
    		}
    
    		// get terms assigned to this related post
    		$terms = explode( ',', $value->related_terms );
    
    		foreach ( $terms as $term ) {
    
    			// check if the post term is a genre term
    			if ( in_array( $term, $genre_term_ids ) ) {
    
    				// reduce related post score with 0.5
    				$results[ $key ]->score[0] = $results[ $key ]->score[0] - 0.5;
    			}
    		}
    	}
    
    	// re-order related posts with new scores
    	uasort(  $results, 'km_rpbt_related_posts_by_taxonomy_cmp' );
    
    	// use original posts_per_page from the widget
    	if ( isset( $args['_posts_per_page'] ) ) {
    		$results = array_slice( $results, 0, absint( $args['_posts_per_page'] ) );
    	}
    
    	return $results;
    }
    
    // Adds the terms assigned to related posts to the results
    add_filter( 'related_posts_by_taxonomy_posts_clauses', 'rpbt_add_terms_to_results', 10, 4 );
    
    function rpbt_add_terms_to_results( $pieces, $post_id, $taxonomies, $args ) {
    	global $wpdb;
    
    	$pieces['select_sql'] .= " , GROUP_CONCAT( DISTINCT tt.term_id SEPARATOR ',' ) as related_terms ";
    
    	return $pieces;
    }

    Change the genre term IDs in this line to your genre term ids.

    $genre_term_ids = array( 12, 13, 40, 41, 56 );

    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.

    Thread Starter Agrajag27

    (@agrajag27)

    It failed. Perhaps I don’t get the ID’s? Remember, these are tags. I got the tag ID’s from the Tag cloud which shows the ID’s. Here’s my ID line:

    $genre_term_ids = array( 151,930,1085,1342,2304,2305,2316,2317,2319,2327,2350,2351,2374 );

    That’s all the genres.

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Please check if the tag ids are correct by using this method

    Go to Posts > Tags and click on the tag name. The edit tag page will open and you will then be able to see the tag ID by looking onto your browser’s address bar. You will notice a URL similar to this:

    yoursite.com/wp-admin/edit-tags.php?action=edit&taxonomy=post_tag&tag_ID=68&post_type=post

    The number in tag_ID=68 is the id of the tag.

    There are also plugins that show the ID.
    https://wordpress.org/plugins/reveal-ids-for-wp-admin-25/

    How did it fail. Were the post not ordered as it should or did something else go wrong

    Thread Starter Agrajag27

    (@agrajag27)

    Verified. Those are the correct tag numbers.

    Found the issue. Somehow an errant character got copied (a ?) before the opening //

    Sorry about that.

    I’ll see how this responds. Thanks!

    This should end up in Recipes. Seems a good start for this.

    Thread Starter Agrajag27

    (@agrajag27)

    Oops. That won’t work. The page took forever to load and then I realized why. It returned like EVERY related page!

    That would again be VERY useful on the back-end type tool I mentioned. I’d LOVE to see this for testing purposes.

    Plugin Author keesiemeijer

    (@keesiemeijer)

    It returned like EVERY related page!

    It didn’t return the number of related posts used in the widget?

    Thread Starter Agrajag27

    (@agrajag27)

    Nope. That’s set to 8. There were little dozens and dozens of returns. As soon as I removed the code it went back to the 8.

    Perhaps it has to do with your test vs. my reality:

    $genre_term_ids = array( 151, 1009, 1132, 1191, 1325, 1342, 2304, 2305, 2316, 2317, 2319, 2323, 2327, 2350, 2351, 2374, 2444 );

    That’s a lot of tags maybe (17)?

    Plugin Author keesiemeijer

    (@keesiemeijer)

    It shouldn’t be all posts that get returned no matter how many tags there are. Let me check the code again.

    Do you use any of these filters in your functions.php (that are not in my code above)?
    https://keesiemeijer.wordpress.com/related-posts-by-taxonomy/filters/

    Thread Starter Agrajag27

    (@agrajag27)

    This is all:

    add_filter( ‘related_posts_by_taxonomy_cache’, ‘__return_true’ );
    add_filter( ‘related_posts_by_taxonomy_display_cache_log’, ‘__return_true’ );

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Can you remove these filters and test again.

    It seems it doesn’t work with the cache in place. I have to rethink the cache implementation for now. I will look into it tomorrow.

    Thread Starter Agrajag27

    (@agrajag27)

    On it…. Will report back when done.

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Weighted Tag Feature’ is closed to new replies.