My client has dragged around the sub-categories in the admin panel to change their order.

I have the following code that outputs the sub-categories of a specific category parent:

$mainCategory = get_categories(
        array (
          'parent'    => $category_id['term_id'],
          'taxonomy'  => 'product_cat',
          'orderby'   =>  'menu_order'
      foreach( $mainCategory as $mc ) {
        $cat_link = get_category_link( $mc->term_id );
        echo '<div class="main-category col-md-3">';
        echo '<a href="'.$cat_link.'">';
        $thumbnail_id = get_woocommerce_term_meta( $mc->term_id, 'thumbnail_id', true ); // Get Category Thumbnail
        $image = wp_get_attachment_url( $thumbnail_id ); 
        if ( $image ) {
          echo '<div class="mc-img">';
          echo '<img src="' . $image . '" alt="" />';
          echo '</div>';
        echo '<h2>';
        echo $mc->name;
        echo '</h2>';
        echo '<p>';
        echo $mc->description;
        echo '</p>';
        echo '<div class="mc-button">Explore Products</div>';
        echo '</a>';
        echo '</div>';

The categories are output in alphabetical order, rather than the order specified by the admin in the dashboard.

'menu_order' does not work


Answering for posterity, since I ran into the same issue and this thread is the closest thing I found to what I was looking for.

The solution turned out to be quite simple.

If you don't specify 'orderby' in your arguments, the default sort order will be used (the category order in the Woo admin). Just remove that part of your arguments.

| improve this answer | |

Why don't you use the Navigational Menu in Wordpress. This way your client can add new terms to the custom menu in the order they so choose it will be reflected as such on your page.

Create a new custom menu in your functions.php file,

function register_my_menu() {
  register_nav_menu('product-list-menu',__( 'Product Listing' ));
add_action( 'init', 'register_my_menu' );`

Then go to your dashboard menu section, Appearance->Menu, and add your custom taxonomy terms or sub-category terms in the order you wish to list them. (Here is a tutorial if you need more info).

Finally you can display your menu on your page as a list, using

<?php wp_nav_menu( array( 'theme_location' => 'product-list-menu' ) ); ?>

the wp_nav_menu function (codex documentation) allows you to add your own css class to custom style your list.

Wordpress uses a Walker class to build menus. Building each item as a list. You can hook into the default Walker class, using the nav_menu_link_attributes (codex documentation) to customise the html attributes for each menu item, and the wp_nav_menu_items to add custom items at the end of the menu list (tutorial).

If you want even more control over the way your list is created (for example use <h2> tags for your term names, then use a custom Walker class which you can parse to the function. Having your own Walker class can make for rather powerful functionality, for example if new terms are added to your taxonomy and these have not been added to your menu, then you can verify this at the time of building the list, allowing you to add new items which have not been ordered at the end of the list.

Hope this helps.

| improve this answer | |
  • The order has already been arranged by the admin using – Alexnl Oct 19 '16 at 13:52
  • Sorry I mean to add more to the last comment and accidentally hit enter. The order has already been arranged via /wp-admin/edit-tags.php?taxonomy=product_cat&post_type=product by dragging the categories around. It isn't ideal to create a menu and keep two separate lists. I've already output the categories with layout and styling, I just need to set the order so that it matches what the admin arranged. grosche.ca/dev/coffee Thanks for the help! – Alexnl Oct 19 '16 at 13:54
  • Based on my research the order may be contained on woocommerce_term_meta but I haven't been able to find the documentation or example of retrieving it from this – Alexnl Oct 19 '16 at 13:56
  • Did you take a look at the DB table to make sure the custom order is being saved in a column? It it does you could do a search in the woocommerce plugin code for the matching column name to see how it is being retrieved. The code has either a function or a hook to let you access that functionality. Sorry, can't be of more help as I have never worked with WooCommerce till date. – Aurovrata Oct 19 '16 at 15:21

I am adding another answer here for your information which is quite different from the first one I provided, hence for clarity I am putting it as 2nd answer.

You can use a plugin to order terms within taxonomies. I cam across the Custom Taxonomy Order New Edition in an article recently which I am yet to try. Searching on google I also came across the Category Order and Taxonomy Terms Order which seems to be doing something similar. You might want to give these a spin to see if they meet your needs.

| improve this answer | |
  • I would prefer not to use a plugin, but will go this route if I can't use another way. – Alexnl Oct 19 '16 at 13:56
  • I have just been testing the Custom Taxonomy Order New Edition plugin I linked above and it works quite well. Haven't tried the other yet, but it has a lot more active installations. – Aurovrata Oct 19 '16 at 15:16

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.