How to make all posts in a category “unsticky”?

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”.

Leave a Comment