我知道发布插件作为解决方案有点不好,但是There Can Only Be One plugin 似乎正是你想要的。
它使用的解决方案(这几乎就是整个插件):
add_action( \'draft_to_publish\', \'only_one_sticky\' );
add_action( \'future_to_publish\', \'only_one_sticky\' );
add_action( \'new_to_publish\', \'only_one_sticky\' );
add_action( \'pending_to_publish\', \'only_one_sticky\' );
add_action( \'publish_to_publish\', \'only_one_sticky\' );
function only_one_sticky( $post_id ) {
if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! wp_is_post_revision( $post_id ) ) {
$post_id = $post_id->ID;
}
$sticky = ( isset( $_POST[\'sticky\'] ) && $_POST[\'sticky\'] == \'sticky\' ) || is_sticky( $post_id );
if( $sticky ) {
$sticky_posts = array();
$sticky_posts_list = get_option( \'sticky_posts\', array() );
// The Post IDs are stored in the options table as a single list, so we need to construct a new list with the future posts, plus the newly-published sticky post.
$new_sticky_posts_list = array();
foreach ($sticky_posts_list as $sticky_post) {
$postStatus = get_post_status ( $sticky_post );
if ( get_post_status ( $sticky_post ) != \'publish\' || $sticky_post == $post_id ) {
array_push( $new_sticky_posts_list, $sticky_post );
}
}
update_option( \'sticky_posts\', $new_sticky_posts_list );
}
}