• Resolved cloud5000

    (@cloud5000)


    Hi, I’m a beginner who is customising jetpack’s related post module through the code provided on this link: https://jetpack.com/support/related-posts/customize-related-posts/

    I’d like to exclude multiple categories from appearing in the related posts module, however, the code below is only for one category.

    function jetpackme_filter_exclude_category( $filters ) {
        $filters[] = array(
            'not' => array(
                'term' => array(
                    'category.slug' => 'dogs',
                ),
            ),
        );
     
        return $filters;
    }
    add_filter( 'jetpack_relatedposts_filter_filters', 'jetpackme_filter_exclude_category' );

    I am currently replacing the slug ‘dogs’ for the slug of the category I want to exclude, but I have more than one category I’d like to exclude.

    I’m not sure how to add new categories to this code and was wondering if you could share similar code but for multiple (2+) categories.

    Thank you

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Jen H. (a11n)

    (@jenhooks)

    Hey @cloud5000,

    This should work for you:

    
    function jetpackme_filter_exclude_category( $filters ) {
        $filters[] = array(
            'not' => array(
                'terms' => array(
                    'category.slug' => array(
                        'dogs',
                        'anothercategory',
                        'yetanothercategory',
                    ),
                ),
            ),
        );
        return $filters;
    }
    add_filter( 'jetpack_relatedposts_filter_filters', 'jetpackme_filter_exclude_category' );
    

    If you don’t see the change showing up right away, you might be able to speed it along by running a new sync from this page in WordPress.com. You’d click the “initiate a sync manually” link there. If that still doesn’t make the changes show up, give it 24 hours. ??

    Let us know how it goes!

    Thread Starter cloud5000

    (@cloud5000)

    Thank you very much Jen, i’ll give this a try ??

    Thread Starter cloud5000

    (@cloud5000)

    Thank you, I just want to confirm that this code works.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Jetpack Related Posts: Exclude Multiple Categories’ is closed to new replies.