• Resolved burlyqlady

    (@burlyqlady)


    I seem to be experiencing a timeout problem in the related posts. Check out our last exchange here: https://wordpress.org/support/topic/show-percentagenumber-of-related-terms-in-common?replies=40
    as the plugin was customized to my site, if you’ll remember.

    I’m frequently seeing related posts that are maybe 30% related (and newly published), when I know there’s an older post that’s 100% related. I tested my theory by publishing a new test post with tags that matched a separate post 100%. It worked and showed the related post in the widget, followed by a 100% match. It wasn’t until I changed the published post date to last year that the match completely disappeared from the related post.

    How can we fix this issue? I want your plugin to consider ALL posts, no matter how old they are and to search for a 100% match no matter what.

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

Viewing 15 replies - 1 through 15 (of 21 total)
  • Thread Starter burlyqlady

    (@burlyqlady)

    Oh I forgot to mention that my settings don’t include a limit on how far back to search through posts:

    $defaults = array(
    		'post_types' => 'post', 'posts_per_page' => 5, 'order' => 'DESC',
    		<strong>'fields' => '', 'limit_posts' => -1, 'limit_year' => '',</strong>
    		'limit_month' => '', 'orderby' => 'post_date',
    		'exclude_terms' => '', 'include_terms' => '',  'exclude_posts' => '',
    		'post_thumbnail' => '', 'related' => true,
    	);
    Plugin Author keesiemeijer

    (@keesiemeijer)

    Did you put this code in your functions.php file?
    https://wordpress.org/support/topic/show-percentagenumber-of-related-terms-in-common?replies=40#post-7022620

    Try this code for the percentage, it’s less expensive. Remove the code you have now first.

    /**
     * Filter used by the Related Posts by Taxonomy plugin.
     */
    add_filter( 'related_posts_by_taxonomy', 'rpbt_add_percentage', 10, 4 );
    
    /**
     * Adds percentage to related post objects.
     */
    function rpbt_add_percentage( $related_posts, $post_id, $taxonomies, $args ) {
    
    	$related_posts = array_reverse( $related_posts );
    	$terms         = count( $args['related_terms'] );
    	$order         = array();
    
    	foreach ( $related_posts as $key => $post ) {
    
    		$related_terms = wp_get_object_terms(  $post->ID, $taxonomies, array( 'fields' => 'ids' ) );
    		$related_terms = count( $related_terms );
    		$percentage    = 0;
    
    		if ( isset( $post->termcount ) ) {
    			$percentage = round( ( (  $post->termcount / $related_terms ) * 100 ) );
    
    			if ( $terms > $related_terms ) {
    				$percentage = round( ( (  $post->termcount / $terms ) * 100 ) );
    			}
    		}
    
    		$order[ $key ] = $percentage;
    		$related_posts[ $key ]->percentage = $percentage;
    	}
    
    	// sort the posts by percentage
    	array_multisort( $order, SORT_DESC, $related_posts );
    
    	return $related_posts;
    }
    
    /**
     * Filter used by the Related Posts by Taxonomy plugin.
     */
    add_filter( 'related_posts_by_taxonomy_caption', 'rpbt_add_in_common_percentage', 10, 3 );
    
    /**
     * Adds the percentage to thumbnail captions.
     */
    function rpbt_add_in_common_percentage( $caption, $post, $args ) {
    
    	if ( isset( $post->percentage ) ) {
    		$caption .= ' (' .  $post->percentage . '%)' ;
    	}
    
    	return $caption;
    }

    Thread Starter burlyqlady

    (@burlyqlady)

    This is the code I had in there, I’ve replaced it with the one you sent me just now.. let me see if it works..

    /**
     * Filter used by the Related Posts by Taxonomy plugin.
     */
    add_filter( 'related_posts_by_taxonomy_caption', 'rpbt_add_in_common_percentage', 10, 3 );
    
    /**
     * Adds the percentage to thumbnail captions.
     */
    function rpbt_add_in_common_percentage( $caption, $post, $args ) {
    
    	if ( isset( $post->percentage ) ) {
    		$caption .= ' (' .  $post->percentage . '%)' ;
    	}
    
    	return $caption;
    }
    Plugin Author keesiemeijer

    (@keesiemeijer)

    This should also be removed. (should be in your theme’s functions.php file as well)

    /**
     * Filter used by the Related Posts by Taxonomy plugin.
     */
    add_filter( 'related_posts_by_taxonomy', 'rpbt_set_taxonomies_global', 10, 4 );
    
    /**
     * Sets the $rpbt_taxonomies global for use in other filters.
     */
    function rpbt_set_taxonomies_global( $related_posts, $post_id, $taxonomies, $args ) {
    
    	if ( 1 === count( $taxonomies ) ) {
    		// The query for the post terms is already cached for a single taxonomy.
    		$terms = get_the_terms( $post_id, $taxonomies[0] );
    	} else {
    		// Not a cached query for multiple taxonomies.
    		$terms = wp_get_object_terms( $post_id, $taxonomies, array( 'fields' => 'ids' ) );
    	}
    
    	if ( is_wp_error( $terms ) || empty( $terms ) ) {
    		return $related_posts;
    	}
    	$related_posts = array_reverse( $related_posts );
    
    	$terms = count( $terms );
    	$order = array();
    
    	foreach ( $related_posts as $key => $post ) {
    
    		$related_terms =  wp_get_object_terms(  $post->ID, $taxonomies, array( 'fields' => 'ids' ) );
    		$related_terms = count( $related_terms );
    
    		$percentage = round( ( (  $post->score[0] / $related_terms ) * 100 ) );
    
    		if ( $terms > $related_terms ) {
    			$percentage = round( ( (  $post->score[0] / $terms ) * 100 ) );
    		}
    
    		$order[ $key ] = $percentage;
    		$related_posts[ $key ]->percentage = $percentage;
    	}
    
    	// sort the posts by percentage
    	array_multisort( $order, SORT_DESC, $related_posts );
    
    	return $related_posts;
    }

    Thread Starter burlyqlady

    (@burlyqlady)

    I’m sorry, I’m confused. Which code should I have in functions.php?

    Plugin Author keesiemeijer

    (@keesiemeijer)

    This code:
    https://wordpress.org/support/topic/timeout-problem-maybe?replies=6#post-7438324

    This is the code I had in there, I’ve replaced it with the one you sent me just now.. let me see if it works..

    That was only a partial part of the code that was already in there.

    Thread Starter burlyqlady

    (@burlyqlady)

    I’ve replaced the code (sorry about the copy and paste error, that was my fault).

    I tested it again and it’s still not showing me older posts that are 100% matches.. here’s an example:

    Post 1 (test post) and post 2 have the same two terms and only those terms so they should match 100%. You can see one is matched on one page, but not the other due to the fact (I think?) post 2 is from last year, so it doesn’t seem to search that far back for a match.

    Shouldn’t Post 2 show up as related in the sidebar to the test post? It’s not there, despite it being a 100% match.

    I hope that wasn’t too confusing!

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Is this code in your functions.php file? (from the other thread)
    https://wordpress.org/support/topic/show-percentagenumber-of-related-terms-in-common?replies=40#post-7022620

    If so remove it.

    Thread Starter burlyqlady

    (@burlyqlady)

    No, it isn’t. I’ve never used that filter because I’ve never wanted to limit the plugin that way.

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Ok, I think I know why it didn’t return the correct posts.

    Remove the old code and try it with this:

    /**
     * Filter used by the Related Posts by Taxonomy plugin.
     */
    add_filter( 'related_posts_by_taxonomy_widget_args', 'rpbt_return_all_posts', 10, 2 );
    
    function rpbt_return_all_posts( $args, $instance ) {
    
    	$args['_posts_per_page'] = $args['posts_per_page'];
    	$args['posts_per_page'] = -1;
    
    	return $args;
    }
    
    /**
     * Filter used by the Related Posts by Taxonomy plugin.
     */
    add_filter( 'related_posts_by_taxonomy', 'rpbt_add_percentage', 10, 4 );
    
    /**
     * Adds percentage to related post objects.
     */
    function rpbt_add_percentage( $related_posts, $post_id, $taxonomies, $args ) {
    
    	$related_posts = array_reverse( $related_posts );
    	$terms         = count( $args['related_terms'] );
    	$order         = array();
    
    	foreach ( $related_posts as $key => $post ) {
    
    		$related_terms = wp_get_object_terms(  $post->ID, $taxonomies, array( 'fields' => 'ids' ) );
    		$related_terms = count( $related_terms );
    		$percentage    = 0;
    
    		if ( isset( $post->termcount ) ) {
    			$percentage = round( ( (  $post->termcount / $related_terms ) * 100 ) );
    
    			if ( $terms > $related_terms ) {
    				$percentage = round( ( (  $post->termcount / $terms ) * 100 ) );
    			}
    		}
    
    		$order[ $key ] = $percentage;
    		$related_posts[ $key ]->percentage = $percentage;
    	}
    
    	// sort the posts by percentage
    	array_multisort( $order, SORT_DESC, $related_posts );
    
    	if ( isset( $args['_posts_per_page'] ) ) {
    		$related_posts = array_slice( $related_posts, 0, $args['_posts_per_page'] );
    	}
    
    	return $related_posts;
    }
    
    /**
     * Filter used by the Related Posts by Taxonomy plugin.
     */
    add_filter( 'related_posts_by_taxonomy_caption', 'rpbt_add_in_common_percentage', 10, 3 );
    
    /**
     * Adds the percentage to thumbnail captions.
     */
    function rpbt_add_in_common_percentage( $caption, $post, $args ) {
    
    	if ( isset( $post->percentage ) ) {
    		$caption .= ' (' .  $post->percentage . '%)' ;
    	}
    
    	return $caption;
    }

    Thread Starter burlyqlady

    (@burlyqlady)

    Did you forget something in there? Because this code broke my site. It returned a blank white page and nothing loaded.

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Can you paste and submit the contents from your functions.php file in a pastebin and post a link to it here.

    http://pastebin.com/

    Thread Starter burlyqlady

    (@burlyqlady)

    Sure. Here it is: http://pastebin.com/5Q9m99W9

    I’m sure I messed up something due to my copying and pasting over and over..

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Add this

    /**

    above this

    * Modify Admin Post Navigation to allow and disallow certain post statuses from being navigated.

    Thread Starter burlyqlady

    (@burlyqlady)

    That did not seem to work 🙁

Viewing 15 replies - 1 through 15 (of 21 total)
  • The topic ‘Timeout problem, maybe??’ is closed to new replies.