save_post_{CPT} not updating the 'sticky_posts' option

add_action( save_post_{CPT}, 'update_sticky', 15, 1 );
function update_sticky( $post_id ) {

    $stickies = get_option( 'sticky_posts' );
    if ( ! in_array( $post_id, $stickies ) ) {
        $stickies[] = $post_id;
        update_option( 'test', $stickies);
        update_option( 'sticky_posts', $stickies );
    }
}
  • It does work for test option I created for testing, but it doesn't work for existing sticky_posts option, although everything is the same.
  • It does work for both if I hardcode the $post_id, like: $stickies[] = 123;
  • Tried it in functions.php file, as well as in the class in which I am registering the CPT, same results - test is working, while sticky_posts is not.
  • Tried different action priorities, 5, 10, 15, 99

Any ideas?

Topic save-post sticky-post options Wordpress

Category Web


Most likely what you're are experiencing is that within the edit_post() the updated post is automatically removed from the sticky posts. This happens for users that can publish posts and edit others post's, after the post is updated and the save_post hooks have fired.

Note that core uses helper functions like stick_post(), unstick_post() and is_sticky() to do this kind of task.

As far as I understand the sticky posts feature is only fully implemented for the the built-in post type. So I'm not sure it's a good idea to mix it with other custom post types.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.