Using WordPress ‘post_stuck’ PHP action

The post_stuck WordPress PHP action fires once a post has been added to the sticky list.

Usage

add_action('post_stuck', 'your_function_name', 10, 1);

function your_function_name($post_id) {
    // Your custom code here
}

Parameters

  • $post_id (int) – ID of the post that was stuck.

More information

See WordPress Developer Resources: post_stuck

Examples

Send an email notification when a post is stuck

Send an email to the site administrator when a post has been made sticky.

function send_email_on_post_stuck($post_id) {
    $to = get_option('admin_email');
    $subject = 'A post has been made sticky';
    $message = 'Post with ID ' . $post_id . ' has been made sticky.';
    wp_mail($to, $subject, $message);
}
add_action('post_stuck', 'send_email_on_post_stuck', 10, 1);

Add a custom CSS class to sticky posts

Add a custom CSS class to the post_class when a post is stuck.

function add_sticky_class($classes, $class, $post_id) {
    if (is_sticky($post_id)) {
        $classes[] = 'custom-sticky-class';
    }
    return $classes;
}
add_filter('post_class', 'add_sticky_class', 10, 3);

Log the date and time when a post is made sticky

Store the date and time when a post is made sticky in the post meta.

function log_post_stuck_date($post_id) {
    update_post_meta($post_id, '_sticky_date', current_time('mysql'));
}
add_action('post_stuck', 'log_post_stuck_date', 10, 1);