0

The other day I posted this question. Now I'm wondering how I can do the same thing except this time instead of making unsticky posts sticky the code should make the ones that already are marked as sticky (in the specified category) unsticky.

I hope my question is clear.

Thanks for all your help!

EDIT: I actually figured out a way to almost make it work. The things I changed from the original code was to make it check if the post is already sticky (instead of checking that it isn't). Then I added an 'unset' to the actual action that the code performs.

Anyway, here's the reformed code:

/**
 * Un-sticky all posts in category 16
 */

add_action( 'save_post', 'mark_post_unsticky' );
function mark_post_unsticky( $post_id ) {

    if ( !wp_is_post_revision( $post_id ) && isset($_POST['sticky']) &&
        in_category('16',$post_id) ) {

        unset($_POST['sticky']);        
    }
}

The only problem now is that the change only happens when I manually edit the article and publish it again. So basically, I need a new "trigger" for it, could anyone please help with that? Thanks!

0

I think the problem is that you have some posts already set as sticky and you don't want to have to go back through and resave them. That being the case you just need to the remove the post IDs from the "sticky" option.

function unset_sticky_for_category($cat) {
  $sticky = get_option( 'sticky_posts' );
  $sp = new WP_Query(
    array(
      'fields' => 'ids',
      'post__in' => $sticky,
      'cat' => $cat // your category to un-sticky
    )
  );

  if (!empty($sp->posts)) {
    $sticky = array_diff($sticky,$sp->posts);
  }

  update_option('sticky_posts',$sticky);
}
unset_sticky_for_category(16);

You should only need to run that once, so long as the code above is preventing new posts in the chosen category from being set to "sticky".

|improve this answer|||||
  • Thank you! I don't know quite enough php to understand how but your code worked! I pasted it in my functions.php and tested it out. Thanks again! – gburning Aug 4 '13 at 1:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.