apply_filters ( 'publicize_should_publicize_published_post', bool $should_publicize, WP_POST $post )

Determines whether a post being published gets publicized.<p>Side-note: Possibly our most alliterative filter name.</p>

Source file:

View in GitHub

Parameters

$should_publicize

(bool) Should the post be publicized? Default to true.

$post

(WP_POST) Current Post object.



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 );

Have a note to contribute?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Comments

  1. djfloopstt says:

    how to do it for categories just change tag to cat?

  2. 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.