Determines whether a post being published gets publicized. Side-note: Possibly our most alliterative filter name.
Parameters
- $should_publicize
bool Should the post be publicized? Default to true.
- $post
WP_POST Current Post object.
Changelog
How to use this hook
Notes
In the example below, we won’t publicize any post using theprivate 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 );