Bulk Make Posts Sticky or Not Sticky
Adds "Make sticky" and "Remove sticky" to the bulk actions on the Posts screen, so you can pin or unpin many posts at once instead of editing each one.
Opens WPIDE on
Code
add_filter( 'bulk_actions-edit-post', function ( $actions ) {
if ( current_user_can( 'edit_others_posts' ) ) {
$actions['wpide_stick'] = 'Make sticky';
$actions['wpide_unstick'] = 'Remove sticky';
}
return $actions;
} );
add_filter( 'handle_bulk_actions-edit-post', function ( $redirect, $action, $post_ids ) {
if ( ! in_array( $action, array( 'wpide_stick', 'wpide_unstick' ), true ) || ! current_user_can( 'edit_others_posts' ) ) {
return $redirect;
}
foreach ( $post_ids as $post_id ) {
if ( current_user_can( 'edit_post', $post_id ) ) {
'wpide_stick' === $action ? stick_post( $post_id ) : unstick_post( $post_id );
}
}
return add_query_arg( 'wpide_sticky_done', count( $post_ids ), $redirect );
}, 10, 3 );
add_action( 'admin_notices', function () {
if ( ! empty( $_GET['wpide_sticky_done'] ) ) {
printf( '<div class="notice notice-success is-dismissible"><p>Updated sticky status of %d posts.</p></div>', absint( $_GET['wpide_sticky_done'] ) );
}
} );
Notes
Sticky posts show first on the blog page in most themes.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.