WordPress.org

Forums

Category Sticky Post
[resolved] Sticky Reset? (8 posts)

  1. loquos
    Member
    Posted 2 years ago #

    Anytime I go edit a post - whether I add a tag, change the category, or simply add additional text to the post, once I "update" it seems that the sticky category choice is lost, and I have to take an extra step to add the correct category again so that it will stick again.

    Any suggestions on how I can avoid this? I don't foresee it happening much in the future, as I don't have a lot of sticky category posts to make, but I thought I'd let you know about the issue and see if it could be something on my end. Thanks!

    http://wordpress.org/extend/plugins/category-sticky-post/

  2. Robin North
    Member
    Posted 2 years ago #

    I'm also experiencing this issue and would also like to see what can be done about it, cheers!

  3. Robin North
    Member
    Posted 2 years ago #

    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.

  4. Robin North
    Member
    Posted 2 years ago #

    Refactored my refactored code to only prevent disabling of the category the current post is stickied in, and to be a bit more concise:

    /**
     * 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();
    
    	// Get the current sticky category for the post, if any
    	$post_sticky_category = get_post_meta( $post->ID, 'category_sticky_post', true );
    
    	// 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( $post_sticky_category, $category->cat_ID, false ) . ( ( $this->category_has_sticky_post( $category->cat_ID ) && $post_sticky_category !== $category->cat_ID ) ? ' disabled ' : '' ) . '>';
    				$html .= $category->cat_name;
    			$html .= '</option>';
    		} // end foreach
    	$html .= '</select>';
    
    	echo $html;
    
    } // end category_sticky_post_display
  5. tommcfarlin
    Member
    Plugin Author

    Posted 2 years ago #

    Hey guys,

    Apologies for this inconvenience. This should be fixed in the latest version (specifically 1.2).

    Please take a look. If you still have this issue, let me know!

    Oh, and thanks for chiming in with sample code, Robin. Much appreciated!

  6. loquos
    Member
    Posted 2 years ago #

    Thanks!

  7. Robin North
    Member
    Posted 2 years ago #

    Unfortunately, I'm still having this issue with 1.2.1. I can't see from the plugin code how you've tried to work around the issue, am I missing something obvious?

    I've patched my copy with the refactored code I posted previously, which I can confirm as working with 1.2.1.

  8. MiriamNZ
    Member
    Posted 2 years ago #

    Still an issue with version 1.2.1. I'm not up to patching, so will wait for the update.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Category Sticky Post
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic