• Resolved morgentau

    (@morgentau)


    Hi,

    I’m using the plugin in a template with km_rpbt_related_posts_by_taxonomy().

    I added a term ID to the exclude_terms option, and for several minutes I was trying to understand why a post which I know has that ID kept appearing in the results.

    Then I saw that the two posts shared another common term, which explained everything.

    However, it seems to me to be counterintuitive. When you ask for posts with common terms but you ask to exclude a specific, other term, you would expect the function not to return posts which have that term associated with them, regardless of other common terms.

    It’s like saying e.g. “See a list of other cities which are large and european, but which are not capitals”. By excluding “capital” you’d hope to not see them, even though they might have “large” and “european” in common.

    At least that’s what I was expecting…

    Great stuff nonetheless.

    https://www.ads-software.com/plugins/related-posts-by-taxonomy/

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

    (@keesiemeijer)

    Hi morgentau.

    If you need to totally exclude posts that have the excluded terms see this code example:
    https://keesiemeijer.wordpress.com/related-posts-by-taxonomy/recipes/#excluding_terms

    Thread Starter morgentau

    (@morgentau)

    Thank you! I’m trying to understand why the “exclude” function needs to be that long with all the validation and custom SQL query. Excuse me because I’m not a developer, but isn’t it possible to simply use ‘tax_query’ instead?

    e.g. here:

    https://www.webdevdoor.com/wordpress/get-posts-custom-taxonomies-terms/

    Thanks.

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Yes, use a tax query to get all the posts with the excluded term ids and add them to the ‘exclude_posts’ parameter of the km_rpbt_related_posts_by_taxonomy() function.

    Example [untested]

    $args = array(
    	'fields' => 'ids',
    	'posts_per_page' => -1,
    
    	// example tax query
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'category',
    			'field'    => 'id',
    			'terms'    => array( 22, 88 ),
    		),
    	),
    );
    $posts = get_posts( $args );
    
    $args = array();
    
    if ( !empty( $posts ) ) {
    	$args['exclude_posts'] = $posts;
    }
    
    $related_posts = km_rpbt_related_posts_by_taxonomy( $post->ID, 'category', $args  );

    The code I linked to is if you want this for the shortcode or widget and is quite small compared to the get_posts() function used by WordPress ??

    Thread Starter morgentau

    (@morgentau)

    I’ll try this as soon as I can. Thanks very much again for your support.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Behavior comment/suggestion’ is closed to new replies.