Have debugged this and found that because the plugin enforces a single sticky post per category by means of adding the disabled
attribute to the currently-assigned sticky category in the Sticky Category select menu, updating a post with a sticky category assigned will result in the sticky category custom post meta data being cleared, as disabled select option values are not added to the $_POST array when a form is POSTed in PHP.
To solve this, I’ve refactored category_sticky_post_display
to only add the disabled
attribute when a category currently has a sticky post and that post is not the one currently being edited.
Here’s the code:
/**
* Renders the select box that allows users to choose the category into which to stick the
* specified post.
*
* @param $post The post to be marked as sticky for the specified category.
*/
function category_sticky_post_display( $post ) {
// Set the nonce for security
wp_nonce_field( plugin_basename( __FILE__ ), 'category_sticky_post_nonce' );
// First, read all the categories
$categories = get_categories();
// Build the HTML that will display the select box
$html = '<select id="category_sticky_post" name="category_sticky_post">';
$html .= '<option value="0">' . __( 'Select a category...', 'category-sticky-post' ) . '</option>';
foreach( $categories as $category ) {
$html .= '<option value="' . $category->cat_ID . '" ' . selected( get_post_meta( $post->ID, 'category_sticky_post', true ), $category->cat_ID, false ) . ( ( $this->category_has_sticky_post( $category->cat_ID ) && ! get_post_meta( $post->ID, 'category_sticky_post', true ) ) ? ' disabled ' : '' ) . '>';
$html .= $category->cat_name;
$html .= '</option>';
} // end foreach
$html .= '</select>';
echo $html;
} // end category_sticky_post_display
Additionally, the delete_post_meta
call can be removed from save_category_sticky_post_data
, as update_post_meta
will not add extra rows to the wp_postmeta table if the post already has a post meta object with the same key.