Post Pic

How to rearrange WordPress categories order

A very common problem with WordPress sites is that you can’t easily reaarange category order. Sure, you can use a custom menu, but for some sites you have to reaarrange the order of your categories. This snippet will show you how to do it easily.

The code above will look for a specific category (Don't forget to update line 5) and will replace it on top of all other categories. The code has to be pasted in your functions.php file.

<?php
$categories = get_terms('books_category');

for( $i=0; $i<sizeof($categories); $i++ ){
  if ( $categories[$i]->name == 'New Publications' ) :
    $latest = array($categories[$i]);
    unset($categories[$i]);
  endif;
}

if( isset($latest) )
  array_splice( $categories, 0, 0, $latest );
?>

Thanks to Sebastian for the great hack!

Related Posts

No related posts.

5 Responses

Jan 05 2012 23:49

Thanks for this code. I’ve been wanting to find a solution for rearranging the order of categories for awhile but forgot about it. I”l try this code in my custom functions in my child theme.

Jan 10 2012 00:48

Oh, damn. Thank you very much for this code – I really needed it.

Jan 11 2012 00:12

Would be very nice if you could link to the original post :)

Jan 12 2012 16:37

@Sebastian: Done, sorry I thought wpsnippet were the original.

Jan 31 2012 22:24

You can use the category ID. It you change the category name the code will break.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required