0

I am trying to figure out how to display the categories in a specific order. Let's say my category IDs are in the following order: 1,3,4,2,5

I want to display the categories in that sequence. Here is the following code I have so far but it isn't working:

$args = array(
    'type'                     => 'post',
    'child_of'                 => 0,
    'parent'                   => '',
    'orderby'                  => 'front_order_id',
    'order'                    => 'ASC',
    'hide_empty'               => 0,
    'hierarchical'             => 1,
    'exclude'                  => 1,
    'include'                  => '1,3,4,2,5',
    'number'                   => '',
    'taxonomy'                 => 'category',
    'pad_counts'               => false
);
$categories = get_categories($args);
1
  • Usually if I need my categories or terms in a specific order I'll refer to one of my questions which allows me to set a custom field to each term where I can define an order (similar to Pages page-order attribute) I can then order by term_group. - View Question / Answer. The order is entirely custom and can easily be changed in the backend.
    – Howdy_McGee
    Commented Oct 15, 2014 at 14:29

2 Answers 2

2

EDIT

I don't really know of a solution within Wordpress to sort the way you need. I think the best is to make use of php sorting to sort your returned array as needed

Here is how:

  1. Get the array of category info as you normally do

    $categories = get_categories($args);
    

$categories will hold an array of the five categories returned from the database

  1. You now need to create an array with your desired sort order

    $keys = [1,3,4,2,5];
    
  2. You are now going to combine $keys and $categories with array_combine. We will use the values of Skeys for the keys for our new combined array

    $categories_rearranged = array_combine( $keys, $categories );
    
  3. At this stage, your array will not be naturally sorted, so with the use of ksort, sort your array so that it is ordered naturally

    ksort($categories_rearranged);
    
  4. $categories_rearranged will now be the array that you will make use of and will be passed to your foreach loop

    foreach ( $categories_rearranged as $category ) {
       echo $category->name;
    }
    

ORIGINAL ANSWER

The codex is there to help you. Look at get_categories() and in particular to the following parameters

orderby

(string) Sort categories alphabetically or by unique category ID. The default is sort by name. Valid values:

  • id

  • name - default

  • slug

  • count

  • term_group

You can see you are passing an invalid parameter to orderby

The include and exclude parameters cannot be used together as well

2
  • The categories I want are 1,3,4,2,5 in that specific order not ASC or desc. Wordpress will instead display the categories in this order 1,2,3,4,5 Commented Oct 15, 2014 at 11:06
  • Please see my edit, I misread your OP. My apologies Commented Oct 15, 2014 at 14:26
0

Can you show what you're getting?

If you're not getting anything, this is probably why:

'child_of' => 0

Also, what's this?

'orderby' => 'front_order_id',

Also, this doesn't change the order:

'include' => '1,3,4,2,5',

check this out http://codex.wordpress.org/Function_Reference/get_categories

2
  • orderby front_order_id is a custom field using the following plugin github.com/bainternet/My-Meta-Box Commented Oct 15, 2014 at 11:04
  • 1
    @stargatesg1 that info should be in your question. Please make sure that all relevant info is posted in your question. This will ensure that no one wastes their time answering irrelevant or erronous info. Thank you Commented Oct 15, 2014 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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