Support » Plugin: Jetpack by WordPress.com » Related Posts: exclude a category from appearing, code provided doesnt’ work

  • Resolved Annonnimmo

    (@annonnimmo)


    Code from this Jetpack official page doesn’t work.
    It still shows results from the excluded category.

    function jetpackme_filter_exclude_category( $filters ) {
    	$filters[] = array( 'not' =>
    	  array( 'term' => array( 'category.slug' => 'food' ) )
    	);
    	return $filters;
    }
    add_filter( 'jetpack_relatedposts_filter_filters', 'jetpackme_filter_exclude_category' );
Viewing 8 replies - 1 through 8 (of 8 total)
  • Hi @annonnimmo

    The filter you are using will get you related posts excluding the one from the category that you don’t want.

    Can you make sure that whether the post is WordPress default posts or some other custom post type? If it is the default post then please check the slug of the category term i.e food you are using in your code above.

    If it is a custom post type then category.slug won’t work. It should be like 'taxonomy.name-of-the taxonomy-goes-here.slug' instead of 'category.slug' in your above code.

    Hope this helps and works for you. If not please let us know again.

    Thank you.
    Best Regards.

    Hi @alokstha1,
    I confirm that the post is WordPress default posts and the slug of the category term is correct. Anyway it doesn’t work.

    Thank you.

    Hi @annonnimmo,
    I’m sorry to say that it is working for me. I’m not sure what could possibly go wrong on your end.

    I would like you to give the last shot and look into the posts that you want to exclude is assigned to that category or not.

    I’ll keep looking into similar threads like yours that already exists in the support forum and will let you know if I find anything that works for you.

    And maybe the plugin moderator will see this ticket and reply.

    Thank you.

    Hi @alokstha1,
    I confirm that the posts I want to exclude are assigned to the right category (with the right slug).

    What version of Jetpack are you using?

    You are very kind, thank you.

    Hi @annonnimmo,
    Thank you for the compliment :).

    I’m using the latest version of Jetpack.

    If this is not working for you then, you can also use the filter hook to exclude certain posts from the related post by its post_id like below codes.

    function exclude_posts_related( $exclude_post_ids, $post_id ) {
        $exclude_post_ids = array(2750); // Exclude the post with post's ID
        return $exclude_post_ids;
    }
    add_filter( 'jetpack_relatedposts_filter_exclude_post_ids', 'exclude_posts_related', 20, 2 );

    Please let me know if this works for you.

    Best Regards.

    I have a similar problem. I am trying to use the code from your reply to exclude a custom category:

    /**
    *
    * Exclude an entire category from ever appearing among Related Posts results
    *
    */
    function jetpackme_filter_exclude_category( $filters ) {
    $filters[] = array( ‘not’ =>
    array( ‘term’ => array( ‘taxonomy.ld_course_category.slug’ => ‘branded’ ) )
    );
    return $filters;
    }
    add_filter( ‘jetpack_relatedposts_filter_filters’, ‘jetpackme_filter_exclude_category’);

    Hi @psychmaster,
    I think you should start a new support ticket.

    But FYI, your function is right. I guess this has something to do with your WordPress installation or Jetpack settings1.

    May be a plugin team will get to this ticket.

    Thank you.

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    These are support topics; we don’t have any tickets here. 😉

    @psychmaster If you need support then per the forum guidelines please start your own topic.

    https://wordpress.org/support/guidelines/#post-in-the-best-place

    You can do so here.

    https://wordpress.org/support/plugin/jetpack/#new-post

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Related Posts: exclude a category from appearing, code provided doesnt’ work’ is closed to new replies.