Search code examples
wordpressforeachwp-list-categories

How to list categories NOT in alphabetical order


Can you help me list categories in the order as they appear in wordpress admin?

So I have the following piece of code in a wp page template, which lists categories for a vendor (user).

            <ul class="category-vendormenu">
            <?php foreach($categories as $category) : ?>
                <?php $liclass = ($current_cat == $category->ID) ? ' class="current"' : ''; ?>
                    <li<?php echo $liclass;?>>
                        <?php $author_posts = new WP_Query( array( 
                            'post_type' => 'product', 
                            'author' => $vendor_id, 
                            'tax_query'=>array(
                                array(
                                    'taxonomy' => 'product_cat', 
                                    'terms' => array($category->ID), 
                                    'field' => 'term_id'
                                    )
                            )
                        ));
                        $count = $author_posts->found_posts;
                        wp_reset_query();

                        ?>
                        <a href="<?php echo $store_url .'section/'. $category->ID ?>" title="<?php echo $category->name ?>">
                            <?php echo $category->name; ?>
                        </a>
                        <?php
                        if ($liclass!='') {
                        $sub_categories = $wpdb->get_results("
                            SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug, tax.count  
                            FROM $wpdb->posts as posts
                            LEFT JOIN $wpdb->term_relationships as relationships ON posts.ID = relationships.object_ID
                            LEFT JOIN $wpdb->term_taxonomy as tax ON relationships.term_taxonomy_id = tax.term_taxonomy_id
                            LEFT JOIN $wpdb->terms as terms ON tax.term_id = terms.term_id
                            WHERE posts.post_status = 'publish' AND
                                posts.post_author = '$vendor_id' AND
                                posts.post_type = 'product' AND
                                tax.taxonomy = 'product_cat' AND
                                tax.parent='".$category->ID."' 
                            ORDER BY terms.name ASC
                        ");
                            if (count($sub_categories)>0) {
                                echo '<ul class="sub-menu">';
                                $term = get_term_by( 'id', $current_cat, 'product_cat', 'ARRAY_A' );
                                wp_register_script( 'ajax-subcategories', get_template_directory_uri() . '/js/ajax_subcategories.js', array( 'jquery' ) );
                                wp_localize_script( 'ajax-subcategories', 'parent_cat', array('ajax_url' => get_template_directory_uri() . '/functions/ajax_subcategories.php', 'store_url'=>$store_url, 'vender_id'=>$vendor_id));
                                wp_enqueue_script ( 'ajax-subcategories');
                                foreach ($sub_categories as $sub_cate) {
                                    $liclass = ($selected_sub_cat == $sub_cate->ID) ? ' class="selected"' : '';
                                    echo '<li'.$liclass.'><a href="'.$store_url .'section/'. $sub_cate->ID.'" title="'. $sub_cate->name.'" onclick="">'.$sub_cate->name.'<span>'. $sub_cate->count .'</span></a></li>';
                                }
                                echo "</ul>";
                            }
                        }
                        ?>
                    </li>
            <?php endforeach; ?>
        </ul>

I might be pooped and missing the obvious, but I'm getting a list of the categories in alphabetical order, which is not what I want.

In wordpress, the categories appears listed as follows:

  • Accommodations
  • Luxury
  • Festivals
  • Private Events
  • Book your own

But the code outputs:

  • Accommodations
  • Book your own
  • Festivals
  • Luxury
  • Private Events

I would simply like to keep the order as show in the wordpress admin.

However you can help, THANK YOU!


Solution

  • In wp_query you need use this parameter 'orderby' => 'title', 'order' => 'DESC',

    You can research this in this artice https://codex.wordpress.org/Class_Reference/WP_Query just search "Order & Orderby Parameters" this segment http://joxi.ru/L21dLEjc8pNkbm