2

We say if a user has user role author then all his new posts will automaticly be sticky.

How can i do that?

1

It looks like it is possible.

Sticky posts are stored as array of IDs in an option called 'sticky_posts`, and you can modify options using hooks, so... There is a chance that something like this would work:

function fake_sticky_posts_for_author() {
    $user = wp_get_current_user();

    if ( get_current_user_id() && in_array('author', $user->roles) ) {
        // Return the IDs of posts that you want to see as sticky
        // For example this gets posts of currently logged in user
        return get_posts( array(
            'author' => get_current_user_id(),
            'fields' => 'ids',
            'posts_per_page' => -1
        ) );
    }

    return false;
}
add_filter( 'pre_option_sticky_posts', 'fake_sticky_posts_for_author' );
// we use pre_option_sticky_posts in here, since we want to ignore value of this option, so there is no point to load it

On the other hand, if you want to set all posts that are written by any author as sticky, then there's another approach that might be better (more efficient). You can check during save_post if author has given role and set it as sticky if so:

function stick_authors_posts_automatically($post_id) {
    // If this is just a revision, don't do anything
    if ( wp_is_post_revision( $post_id ) )
        return;

    $user = new WP_User( get_post_field ('post_author', $post_id) );

    if ( in_array('author', $user->roles) ) {    
        // stick the post
        stick_post( $post_id );    
    }
}
add_action( 'save_post', 'stick_authors_posts_automatically' );

Disclaimer: This code isn't tested, so there may be some typos, etc. But idea is clear, I hope :)

  • Allmost worked. It made the user role "author" new posts sticky. But its only sticky for him. If i log in with another account then the same post isnt sticky. – ANdy May 23 '18 at 22:21
  • Yes, this code works exactly this way - if current user is an author, then it makes his posts sticky for him... Isn't that what you've asked for? If not, then how should it work? – Krzysiek Dróżdż May 23 '18 at 22:23
  • Make it sticky "globaly" so everyone sees that post as sticky. – ANdy May 23 '18 at 22:30
  • OK, so every post written by author should be sticky for everyone? – Krzysiek Dróżdż May 23 '18 at 22:33
  • Yes that was what i ment in the first post. – ANdy May 24 '18 at 0:43
0
add_action('save_post', 'mo_make_it_sticky_if_role');
function mo_make_it_sticky_if_role( $post_id ) {
    if( current_user_can('author') ) {
        stick_post( $post_id );
    }
}

If the user has user role author and he creates a new post then it gets sticky as default even if he uses somecind of fron-end post creator.
It will only work if the author makes a new post.
His old posts which he created before this function was added will not get sticky.

  • Can you please edit your answer to explain what exactly this code does so future readers and users who may find your code snippet helpful knows what's happening here? – Howdy_McGee May 24 '18 at 21:07
  • Fixed it with my poor english. – ANdy May 24 '18 at 23:04
  • You should not use current_user_can with roles. It is clearly stated in Codex, that the result of such usage is not guaranteed. – Krzysiek Dróżdż May 25 '18 at 10:34
  • Well it worked i have tested it very well. – ANdy May 25 '18 at 11:37
  • It doesn't matter what you've tested. Here is citation from Codex: "While checking against particular roles in place of a capability is supported in part, this practice is discouraged as it may produce unreliable results." – Krzysiek Dróżdż May 26 '18 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.