• Resolved edwards1

    (@edwards1)


    Hi Plugin Manager, thanks for the great work.

    I need help excluding some certain categories from sharing to the connected social media account. Please help ASAP

Viewing 7 replies - 1 through 7 (of 7 total)
  • I would also know that, as some news are not to be shared on social media.

    @edwards1 Thanks for the kind words! You can use a filter to exclude certain categories:

    https://developer.jetpack.com/hooks/publicize_should_publicize_published_post/

    The example references tags, but you can use categories just the same. I hope that helps!

    Thank you for your hint. As i am not sure, would this work then for posts with the category “news” to be excluded from publicize:

    /**
     * Do not trigger Publicize if the post uses the <code>news</code> cat.
     */
    function jeherve_control_publicize( $should_publicize, $post ) {
        // Return early if we don't have a post yet (it hasn't been saved as a draft)
        if ( ! $post ) {
            return $should_publicize;
        }
     
        // Get list of tags for our post.
        $tags = wp_get_post_cats( $post->ID );
     
        // Loop though all cats, and return false if the cat's name is <code>news</code>
        foreach ( $cats as $cat ) {
            if ( 'news' == $cat->name ) {
                return false;
            }
        }
     
        return $should_publicize;
    }
    add_filter( 'publicize_should_publicize_published_post', 'jeherve_control_publicize', 10, 2 );

    And to add a 2nd question .. where exactly do i past that code in?

    @hurz In the future, would you please start your own thread, as per the Forum Welcome?

    https://www.ads-software.com/support/plugin/jetpack#new-post

    You’d need to replace the code:

    $tags = wp_get_post_cats( $post->ID );

    With this:

    wp_get_post_categories( $post->ID, array(‘fields’ => ‘all’ )) ;

    wp_get_post_cats has been deprecated.

    You can either add this code to your functions.php file or you can use a functionality plugin.

    We don’t typically provide support for custom code, so if you’d like additional assistance with this, then you might seek out the help of a developer.

    Cheers!

    Thread Starter edwards1

    (@edwards1)

    Thank you alot for the reply… the categories to exclude is for example, online-videos and sports.

    is this how the code should be?

    /**
    * Do not trigger Publicize if the post uses the online-videos, sports category.
    */
    function jeherve_control_publicize( $should_publicize, $post ) {
    // Return early if we don’t have a post yet (it hasn’t been saved as a draft)
    if ( ! $post ) {
    return $should_publicize;
    }

    // Get list of tags for our post.
    wp_get_post_categories( $post->111, 222 array(‘fields’ => ‘all’ )) ;

    // Loop though all categories, and return false if the category name is online-videos, sports
    foreach ( $categoories as $categories ) {
    if ( ‘online-videos, sports’ == $categories->name ) {
    return false;
    }
    }

    return $should_publicize;
    }
    add_filter( ‘publicize_should_publicize_published_post’, ‘jeherve_control_publicize’, 10, 2 );

    please your help will be much appreciated as we have some content shared to our socials which shouldn’t be.

    @edwards1 This is going a bit outside the level of support we provide here now. We can give you the tools, but we’re not really able to help much with the implementation.

    If you’re not sure how to proceed with this, you may want to enlist the help of an experienced developer.

    That said, if the categories online-videos and sports are separate, then you should add them to the code separately. As it’s written, this function is looking for the category slug online-videos, sports, which I suspect doesn’t exist.

    I also noticed a spelling mistake.

    foreach ( $categoories as $categories ) {

    should be

    foreach ( $categories as $categories ) {

    There’s an extra ‘o’ ??

    I hope that helps!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Exclude a certain category from sharing in publicize’ is closed to new replies.