Source file: modules/publicize/publicize-jetpack.php
View in GitHubParameters
- $should_publicize
-
(bool) Should the post be publicized? Default to true.
- $post
-
(WP_POST) Current Post object.
Changelog
Since: Jetpack 10.9
Notes
In the example below, we won’t publicize any post using the private
tag:
/** * Do not trigger Publicize if the post uses the `private` tag. */ 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_tags( $post->ID ); // Loop though all tags, and return false if the tag's name is `private` foreach ( $tags as $tag ) { if ( 'private' == $tag->name ) { return false; } } return $should_publicize; } add_filter( 'publicize_should_publicize_published_post', 'jeherve_control_publicize', 10, 2 );
You can apply the same idea to ignore a specific category:
/** * Do not trigger Publicize if the post uses the `do-not-publicize` category. * * @param bool $should_publicize Should the post be publicized? Default to true. * @param WP_POST $post Current Post object. */ function jeherve_control_publicize_for_categories( $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 categories for our post. $categories = get_the_category( $post->ID ); if ( is_array( $categories ) && ! empty( $categories ) ) { foreach( $categories as $category ) { if ( 'do-not-publicize' === $category->slug ) { return false; } } } return $should_publicize; } add_filter( 'publicize_should_publicize_published_post', 'jeherve_control_publicize_for_categories', 10, 2 );
You can apply the same idea to stop a specific user’s posts from being publicized:
/** * Do not trigger Publicize for a specific user. * * @param bool $should_publicize Should the post be publicized? Default to true. * @param WP_POST $post Current Post object. */ function ignore_specific_user_posts( $should_publicize, $post ) { // Assuming the user whose posts you want to ignore has the user id 2. if ( get_current_user_id () === 2 ) { return false; } return $should_publicize; } add_filter( 'publicize_should_publicize_published_post', 'ignore_specific_user_posts', 10, 2 );
how to do it for categories just change tag to cat?
You will need to adapt the code a bit more. You can read more about this here:
https://wordpress.org/support/topic/exclude-a-certain-category-from-sharing-in-publicize/
Thanks but excluding categories or tags from publicize should be a feature to prevent spammy behaviour. Is there anyway I could make this a feature request?
This feature is already available via the filter above, but we most likely won’t be adding a setting in the interface for that option, as it’s not something most Jetpack users would be interesting.
You can develop your own interface with that filter, though!
It would be great if you guys could just include the code for categories somewhere like you do for tags to take the guesswork out of it. I’ve tried to implement this several times and get errors every time I try to save the functions.php file. Several people have asked for help with excluding categories.
Things work the same way for categories. I’ve added a snippet above to give you an example.