Support » Plugin: Related Posts by Taxonomy » Find an minimum number of posts

  • Hello! First: Thank you for your work. I’m using your plugin in a rather large site with about 25000 Posts, which are spread over 93 categories. All those categories are in a hierarchy with 2 main categories.

    We want to show a minimum of 6 related posts under each article.

    Now I’ve found out that the plugin does not find enough posts in some of the smaller categories on the 3rd level of the hierarchy. I’ve already tried to give the ids of the parent-categories with the include_terms-parameter, but had no success.

    Here is my code:

    $arguments = array(
            'fields' => 'ids',
            'taxonomies' => array('category', 'post_tag'),
            'posts_per_page' => '10',
            'limit_year' => '4',
            'exclude_terms' => $ignoredCategories
        );
    
        $related_posts = km_rpbt_get_related_posts($post_id, $arguments);

    Is there a possibility to increase the amount of results by adding the parent categories or an option to tell the plugin “find at least 6”?

    Thank you for your help,
    Martin

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

    (@keesiemeijer)

    Hi Martin

    Try it with the terms argument.

    
    $arguments = array(
            'fields' => 'ids',
            'taxonomies' => array('category', 'post_tag'),
            'posts_per_page' => '10',
            'limit_year' => '4',
            'terms' => $ignoredCategories
        );
    
        $related_posts = km_rpbt_get_related_posts($post_id, $arguments);
    

    Does that improve the results?

    Thanks for the hint. I’ve tried this configuration, but it did not find more posts. The Array in ‘terms’ has die IDs of the current, the parent and the main-category. I had no luck.

    $arguments = array(
            'fields' => 'ids',
            'taxonomies' => array('category', 'post_tag'),
            'posts_per_page' => '10',
            'limit_year' => '4',
            'exclude_terms' => $categoriesToIgnore,
            'terms' => array(261, 48, 12)
        );
    
        $related_posts = km_rpbt_get_related_posts($post_id, $arguments);
    • This reply was modified 8 months, 1 week ago by  Martin Schneider. Reason: Code formatting
    Plugin Author keesiemeijer

    (@keesiemeijer)

    I’m not sure, but it seems there are just not enough related posts 🙂

    You could try including all parent terms with this.

    
    $hierarchical = array();
    $taxonomies   = array( 'category', 'post_tag' );
    $terms        = wp_get_object_terms( $post_id, $taxonomies );
    $terms        = ! is_wp_error( $terms ) ? $terms : array();
    
    foreach ( $terms as $term ) {
    	if ( is_taxonomy_hierarchical( $term->taxonomy ) ) {
    		$parents      = get_ancestors( $term->term_id, $term->taxonomy );
    		$hierarchical = array_merge( $hierarchical, $parents );
    	}
    }
    
    $terms = ! empty( $terms ) ? wp_list_pluck( $terms, 'term_id' ) : array();
    $terms = array_merge( $terms, $hierarchical );
    
    $arguments = array(
    	'fields'         => 'ids',
    	'taxonomies'     => array( 'category', 'post_tag' ),
    	'posts_per_page' => '10',
    	'limit_year'     => '4',
    	'exclude_terms'  => $categoriesToIgnore,
    	'terms'          => $terms ? $terms : '',
    	'related'        => false,
    );
    
    $related_posts = km_rpbt_get_related_posts( $post_id, $arguments );
    

    Since my question is related to the above use case, I’ll post it here…

    Using the function km_rpbt_get_related_posts(), is it possible to return the number of posts specified in the posts_per_page parameter, even if not enough related posts are found?

    For example, if posts_per_page is 5 but only 3 related posts are found, the last 2 posts would just be the latest posts.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.