• Resolved Steven

    (@shyzer)


    Using Jetpack’s documentation, I currently have the following code in my functions.php, which excludes the Dead category from appearing in my Jetpack Related Posts widget. And it works great!

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

    However, how do you exclude multiple categories?

    • This topic was modified 6 years, 4 months ago by Steven.
Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Contributor James Huff

    (@macmanx)

    You can just drop the rest in an array inside the array:

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

    Though, for the sake of simplicity, I generally recommend sticking to one dead category that you just assign things when they’re dead. ??

    Thread Starter Steven

    (@shyzer)

    Thanks, James! I didn’t think to put it in another array, so I tried this.

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

    However, after updating the code, Jetpack Relate Posts simply vanishes from my site.

    If I revert back to the original, one category exclusion, it re-appears.

    Thread Starter Steven

    (@shyzer)

    For the time being, I’ve reverted back to excluding only a single category, which makes Jetpack Related Posts show up on my site.

    However, I keep getting an error message of “Could not fetch your site data” when I debug my site.

    I disconnected Jetpack and reinstalled it tonight, as per the documentation, but I still get the could not fetch message.

    I’m not sure if this is related to why Jetpack won’t successfully exclude multiple categories or not though.

    Plugin Contributor James Huff

    (@macmanx)

    No, the problem was a syntax error, sorry about that! This will definitely work:

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

    As for the connection issue, I don’t see anything that’s outright blocking us. Are you running any security plugins?

    Thread Starter Steven

    (@shyzer)

    Thanks for that updated code, James! After uploading it, Jetpack stopped vanishing from my site.

    Do you know if the exclusions should start immediately or if I need to wait for the Jetpack servers to refresh over the course of today? I’m still seeing posts from the excluded categories appear in Related Posts, even after clearing my site’s cache.

    And yes, I am running a security plugin (Sucuri), which might explain the Jetback debug issue I ran into! Good call on that.

    Plugin Contributor James Huff

    (@macmanx)

    It might be best to give it 24 hours, just to see if there aren’t any other caching elements hiding out.

    Thread Starter Steven

    (@shyzer)

    Awesome, I’ll do that and see how things look tomorrow. Thanks again.

    Plugin Contributor James Huff

    (@macmanx)

    Please let us know how it goes! ??

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