Support » Plugin: Jetpack by WordPress.com » Is this correct to exclude certain categories from Publicize?

  • Resolved jakobe75

    (@jakobe75)


    Is this the correct way to exclude these three categories from being auto shared with Publicize?

    My category ID’s are 751,752,753
    and their slugs are:
    guest-blogger1
    guest-blogger2
    guest-blogger3

    add to functions.php

    /**
    * Do not trigger Publicize if the post uses the guest blogger categories.
    */
    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->751,array(‘fields’ => ‘all’ )) ;
    wp_get_post_categories( $post->752,array(‘fields’ => ‘all’ )) ;
    wp_get_post_categories( $post->753,array(‘fields’ => ‘all’ )) ;

    // Loop though all categories, and return false if guest blogger categories.
    foreach ( $categories as $categories ) {
    if ( ‘guest-blogger1’ == $categories->name ) {
    if ( ‘guest-blogger2’ == $categories->name ) {
    if ( ‘guest-blogger3’ == $categories->name ) {
    return false;
    }
    }

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

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    I see you found our documentation about this:
    https://developer.jetpack.com/hooks/publicize_should_publicize_published_post/

    As I mentioned in the comments of the Codex page, you can indeed use the publicize_should_publicize_published_post filter to exclude posts from specific categories being publicized. This was discussed in this support thread:
    https://wordpress.org/support/topic/exclude-a-certain-category-from-sharing-in-publicize/

    You can base yourself off the code in that thread to get something that will work for you.

    If you need more help coming up with the exact code to use on your site, I would recommend hiring a WordPress professional. You can find one on those websites:
    https://jobs.wordpress.net/
    https://jetpack.pro/

    I would like to say that this is indeed a feature that Jetpack users would want.
    […]
    It should be a native and a clear control panel fucntion to include/exclude a category or a tag.
    […]
    This should be an easier process than coding or manually configuring each post.

    That is not something we plan to add to the Publicize panel in the near future, but that is something you may add to your own site if necessary, by building an interface relying on the filter above.

    I hope this helps.

    jakobe75

    (@jakobe75)

    this portion in my code.

    wp_get_post_categories( $post->751,array(‘fields’ => ‘all’ )) ;
    wp_get_post_categories( $post->752,array(‘fields’ => ‘all’ )) ;
    wp_get_post_categories( $post->753,array(‘fields’ => ‘all’ )) ;

    it generatres warning errors when saving

    although this (which excludes a stated post ID) does validate.

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

    Which is correct?
    does the post ID have to implicetly listed for each or is it just happy using the slug name to do the exclusions?

    The example given on the forum and previous posts uses the first example and no comments were made of its useage only a correction for a typo.

    • This reply was modified 1 year ago by .
    jakobe75

    (@jakobe75)

    additionally, I am a paid Jetpack user, so please dont shrug me off. I understand you are busy.

    jakobe75

    (@jakobe75)

    and after adding even the stock code example given for tags, it results in a message that says “Publicize is disabled for this blog” when reactivating the share function.

    jakobe75

    (@jakobe75)

    The example code given does not work, It still auto publishes with the tags example and also my modified categories example. caches cleared, fresh post.

    Whats is the current method of excluding items from Publicize?

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    The filter I mentioned earlier and the example code that is posted in the Codex page work; I have just tested it and can confirm that it behaves as expected. A post tagged “private” is not sent to any of the linked Social Networks you may have set under Settings > Sharing. When you use any other tag, the posts are sent.

    You can consequently use the same filter to exclude posts that belong to a specific category.

    As my colleague mentioned in the thread I linked to, you can use the wp_get_post_categories function to get your post’s categories, instead of getting the post tags with wp_get_post_tags.

    You then have to loop through all those categories, and then shortcircuit (return false) if you find a category that matches your search. By using the fields argument and setting it to all, as explained in the function’s documentation, you will receive an array of WP_Term objects from wp_get_post_categories; that will allow you, when looping through that array, to search for a specific category by name, by slug, or by any other value available inside the WP_Term object (you’ll note that in our example with tags in the Jetpack codex, we were searching for the “private” tag by name, for example).

    I hope this clarifies things a bit.

    If you do not feel comfortable editing PHP code on your own, I would suggest, as my colleague had suggested in the previous thread, that you enlist the help of an experienced developer. Indeed, 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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Is this correct to exclude certain categories from Publicize?’ is closed to new replies.