Title: Timeout problem, maybe??
Last modified: August 30, 2016

---

# Timeout problem, maybe??

 *  Resolved [burlyqlady](https://wordpress.org/support/users/burlyqlady/)
 * (@burlyqlady)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/timeout-problem-maybe/)
 * 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](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/](https://wordpress.org/plugins/related-posts-by-taxonomy/)

Viewing 15 replies - 1 through 15 (of 21 total)

1 [2](https://wordpress.org/support/topic/timeout-problem-maybe/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/timeout-problem-maybe/page/2/?output_format=md)

 *  Thread Starter [burlyqlady](https://wordpress.org/support/users/burlyqlady/)
 * (@burlyqlady)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/timeout-problem-maybe/#post-6552314)
 * 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](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/timeout-problem-maybe/#post-6552522)
 * 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](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](https://wordpress.org/support/users/burlyqlady/)
 * (@burlyqlady)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/timeout-problem-maybe/#post-6552525)
 * 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](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/timeout-problem-maybe/#post-6552526)
 * 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](https://wordpress.org/support/users/burlyqlady/)
 * (@burlyqlady)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/timeout-problem-maybe/#post-6552527)
 * I’m sorry, I’m confused. Which code should I have in functions.php?
 *  Plugin Author [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/timeout-problem-maybe/#post-6552531)
 * This code:
    [https://wordpress.org/support/topic/timeout-problem-maybe?replies=6#post-7438324](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](https://wordpress.org/support/users/burlyqlady/)
 * (@burlyqlady)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/timeout-problem-maybe/#post-6552537)
 * 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)](http://www.skincaredupes.com/ingredient-list/test/) and 
   [post 2](http://www.skincaredupes.com/moisturizer-dupes/neutrogena-oil-free-moisture-sensitive-skin-4-oz/)
   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](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/timeout-problem-maybe/#post-6552544)
 * 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](https://wordpress.org/support/topic/show-percentagenumber-of-related-terms-in-common?replies=40#post-7022620)
 * If so remove it.
 *  Thread Starter [burlyqlady](https://wordpress.org/support/users/burlyqlady/)
 * (@burlyqlady)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/timeout-problem-maybe/#post-6552548)
 * No, it isn’t. I’ve never used that filter because I’ve never wanted to limit 
   the plugin that way.
 *  Plugin Author [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/timeout-problem-maybe/#post-6552551)
 * 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](https://wordpress.org/support/users/burlyqlady/)
 * (@burlyqlady)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/timeout-problem-maybe/#post-6552552)
 * Did you forget something in there? Because this code broke my site. It returned
   a blank white page and nothing loaded.
 *  Plugin Author [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/timeout-problem-maybe/#post-6552553)
 * 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/](http://pastebin.com/)
 *  Thread Starter [burlyqlady](https://wordpress.org/support/users/burlyqlady/)
 * (@burlyqlady)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/timeout-problem-maybe/#post-6552556)
 * Sure. Here it is: [http://pastebin.com/5Q9m99W9](http://pastebin.com/5Q9m99W9)
 * I’m sure I messed up something due to my copying and pasting over and over..
 *  Plugin Author [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/timeout-problem-maybe/#post-6552557)
 * Add this
 *     ```
       /**
       ```
   
 * above this
 *     ```
       * Modify Admin Post Navigation to allow and disallow certain post statuses from being navigated.
       ```
   
 *  Thread Starter [burlyqlady](https://wordpress.org/support/users/burlyqlady/)
 * (@burlyqlady)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/timeout-problem-maybe/#post-6552558)
 * That did not seem to work 🙁

Viewing 15 replies - 1 through 15 (of 21 total)

1 [2](https://wordpress.org/support/topic/timeout-problem-maybe/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/timeout-problem-maybe/page/2/?output_format=md)

The topic ‘Timeout problem, maybe??’ is closed to new replies.

 * ![](https://ps.w.org/related-posts-by-taxonomy/assets/icon.svg?rev=1115231)
 * [Related Posts by Taxonomy](https://wordpress.org/plugins/related-posts-by-taxonomy/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/related-posts-by-taxonomy/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/related-posts-by-taxonomy/)
 * [Active Topics](https://wordpress.org/support/plugin/related-posts-by-taxonomy/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/related-posts-by-taxonomy/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/related-posts-by-taxonomy/reviews/)

 * 21 replies
 * 2 participants
 * Last reply from: [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * Last activity: [10 years, 9 months ago](https://wordpress.org/support/topic/timeout-problem-maybe/page/2/#post-6552591)
 * Status: resolved