• Resolved burlyqlady

    (@burlyqlady)


    Hi. I have two posts with the same exact tags. One post showed up as a related post (in first place) on one page and the other didn’t. It wasn’t until I moved the oldest post up to this year’s date (it was post dated last year) that it “found” the related post.

    Is the plugin searching far back enough for related posts? or does it stop looking for related posts at a certain date? I’m using the widget by the way, with a custom code. Is there any way to have it search through the entire database of posts for related posts?

    Here is the code I’m using:

    /**
     * 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;
    }

    Also, I am using the latest version of the plugin.

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

    (@keesiemeijer)

    Hi burlyqlady

    It should search in all your posts.

    Can you put this in your functions.php. It will list some debug information to admins in the footer of your site.

    add_filter( 'related_posts_by_taxonomy_debug', '__return_true' );

    Can you post the debug information here

    Thread Starter burlyqlady

    (@burlyqlady)

    66 9​ ​
    *******************************************
    ** **
    ** Related Posts by Taxonomy Debug **
    ** **
    *******************************************

    No debug information found

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Is that from a page with a widget or shortcode on it?

    Thread Starter burlyqlady

    (@burlyqlady)

    Oh sorry, that was for the homepage. Here’s the correct info for the widget on the single page I referred to previously:

    *******************************************
    **                                       **
    **    Related Posts by Taxonomy Debug    **
    **                                       **
    *******************************************
    
    type:
    
    widget
    
    -------------------------------------------
    cache:
    
    no persistent cache
    
    -------------------------------------------
    current post id:
    
    43656
    
    -------------------------------------------
    taxonomies used for related query:
    
    'post_tag'
    
    -------------------------------------------
    terms found for current post:
    
    Array
    (
        [0] => 558
        [1] => 564
        [2] => 682
        [3] => 560
    )
    
    -------------------------------------------
    terms used for related query:
    
    Array
    (
        [0] => 558
        [1] => 564
        [2] => 682
        [3] => 560
    )
    
    -------------------------------------------
    related post ids found:
    
    Array
    (
        [0] => 43656
        [1] => 1127
        [2] => 18222
        [3] => 48300
        [4] => 48585
        [5] => 48594
        [6] => 48598
        [7] => 1436
        [8] => 2278
        [9] => 48302
        [10] => 58303
        [11] => 32315
        [12] => 36370
        [13] => 48578
        [14] => 50265
        [15] => 52517
    )
    
    -------------------------------------------
    widget args:
    
    Array
    (
        [post_types] => Array
            (
                [0] => post
            )
    
        [posts_per_page] => -1
        [order] => DESC
        [fields] =>
        [limit_posts] => -1
        [limit_year] =>
        [limit_month] =>
        [orderby] => post_date
        [exclude_terms] =>
        [include_terms] =>
        [exclude_posts] =>
        [post_thumbnail] => 1
        [related] => 1
        [title] => Products with Similar Ingredients
        [taxonomies] => post_tag
        [format] => thumbnails
        [image_size] => thumbnail
        [columns] => 3
        [singular_template] => 1
        [post_id] => 63704
        [random] =>
        [type] => widget
        [caption] => post_title
        [_posts_per_page] => 16
    )
    
    -------------------------------------------
    function args:
    
    Array
    (
        [post_types] => Array
            (
                [0] => post
            )
    
        [posts_per_page] => -1
        [order] => DESC
        [fields] =>
        [limit_posts] => -1
        [limit_year] => 0
        [limit_month] => 0
        [orderby] => post_date
        [exclude_terms] => Array
            (
            )
    
        [include_terms] => Array
            (
            )
    
        [exclude_posts] => Array
            (
                [0] => 63704
            )
    
        [post_thumbnail] => 1
        [related] => 1
    )
    
    -------------------------------------------
    related posts query:
    
    SELECT posts.* , count(distinct tt.term_taxonomy_id) as termcount FROM posts INNER JOIN term_relationships tr ON (posts.ID = tr.object_id) INNER JOIN term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) INNER JOIN postmeta ON ( posts.ID = postmeta.post_id ) WHERE ( ( post_type = 'post' AND ( post_status = 'publish' OR post_status = 'private' ) ) ) AND posts.ID != 63704 AND ( tt.term_id IN (558, 564, 682, 560) ) AND (
      postmeta.meta_key = '_thumbnail_id'
    ) GROUP BY posts.ID ORDER BY posts.post_date DESC 
    
    -------------------------------------------
    requested template:
    
    related-posts-thumbnails.php
    
    -------------------------------------------
    widget:
    
    Array
    (
        [name] => Page Sidebar
        [id] => page-sidebar
        [description] => These widgets appear in the sidebar of the page content.
        [class] =>
        [before_widget] => <div id="related-posts-by-taxonomy-2" class="widget clearfix related_posts_by_taxonomy">(<a href="#rpbt-widget-debug-1">Debug Widget</a>)
        [after_widget] => </div>
        [before_title] => <div class="header clearfix"><div class="bar-label">
        [after_title] => </div></div>
        [widget_id] => related-posts-by-taxonomy-2
        [widget_name] => Related Posts By Taxonomy
    )
    
    -------------------------------------------
    Plugin Author keesiemeijer

    (@keesiemeijer)

    I don’t see a time restriction or limit in the query for related posts, so it should search all posts.

    Do all other related posts that are displayed have a 100% score?

    Thread Starter burlyqlady

    (@burlyqlady)

    Only those that are actually 100% related, but I haven’t checked every single post. It’s just with this one that I noticed.

    Thread Starter burlyqlady

    (@burlyqlady)

    If it helps you, here’s my server info:

    PHP 5.5.33
    php memory 256mb
    php max exec 300
    cURL: on
    50GB Space / 1500GB B/W / 4GB+ RAM / 2 cores
    CentOS 6.x 64 Bit

    Thread Starter burlyqlady

    (@burlyqlady)

    And I have w3 total cache + cloudflare on my site. Object cache is turned off on w3tc though.

    Plugin Author keesiemeijer

    (@keesiemeijer)

    What I meant is on the problem pages. Example:

    Post page d -> has related post a (100%)
    Post page a -> Doesn’t have related post d

    On page a are all related posts 100% ?

    Because the widget is limited to 16 posts it could be post d is the 17th related post ordered by percent and post date.

    Thread Starter burlyqlady

    (@burlyqlady)

    No, that isn’t the case.

    This post didn’t find the 100% related post, which is this one, until I changed the post date to this year. It was originally dated 2015.

    I tried replicating the issue, but it doesn’t seem to be working, I guess since it found the related post already, it’s not likely to “unmatch” it.

    Thread Starter burlyqlady

    (@burlyqlady)

    If that were the case, I don’t understand why post d (the 17th post) wouldn’t be in the first position, as it’s 100% and the sorting is supposed to be DESC by percentage, right?

    Thread Starter burlyqlady

    (@burlyqlady)

    Also, I don’t want post date to be taken into account in the sorting, only the percentage.

    Plugin Author keesiemeijer

    (@keesiemeijer)

    I tried replicating the issue, but it doesn’t seem to be working, I guess since it found the related post already, it’s not likely to “unmatch” it.

    So you tried by setting the post date back for this post?

    This query should always result in the same related posts, there is no such thing as unmatching posts. It’s one query to the database. Maybe your cache is messing things up.

    If that were the case, I don’t understand why post d (the 17th post) wouldn’t be in the first position, as it’s 100% and the sorting is supposed to be DESC by percentage, right?

    That’s why I asked if they were all 100%.

    The posts are first sorted by percentage, then by date.

    Thread Starter burlyqlady

    (@burlyqlady)

    No, they are not all 100%. The next one was 60%.

    Plugin Author keesiemeijer

    (@keesiemeijer)

    I know now, because I asked. Just eliminating possibilities.

    Can you replicate it by setting the date back again?

    I think It could be the w3 total cache plugin showing a cached widget. Any time you edit a post (or update the date), the cache is flushed. that could be the reason why after editing the post date the 100% post was finally displayed. If you can’t replicate the issue by setting the post date back to the original (or even older) date I think it’s the cache

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

The topic ‘Using widget, not finding matching posts, stops at certain date?’ is closed to new replies.