Title: Order category by count
Last modified: August 11, 2019

---

# Order category by count

 *  Resolved [maytro](https://wordpress.org/support/users/maytro/)
 * (@maytro)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/order-category-by-count/)
 * Hello,
 * I’ve created some of category and subcategory in my website.
    The default is 
   to display the category **under each video** (class=”aiovg-link-category” is 
   by alphabet. On category pages, I found how to change it by count instead of 
   the alphabet. But in all the rest I didn’t.
 * Thanks

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Plugin Contributor [wpvideogallery](https://wordpress.org/support/users/wpvideogallery/)
 * (@wpvideogallery)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/order-category-by-count/#post-11821830)
 * Aah. Sorry, the category links displayed under each video doesn’t respond to 
   the categories ordering set in the plugin’s settings page. I promise our next
   version can do this. Please be patient.
 *  Thread Starter [maytro](https://wordpress.org/support/users/maytro/)
 * (@maytro)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/order-category-by-count/#post-11822869)
 * And I can’t do that on the server-side? I’m a programmer
 *  Plugin Contributor [wpvideogallery](https://wordpress.org/support/users/wpvideogallery/)
 * (@wpvideogallery)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/order-category-by-count/#post-11822913)
 * You simply need to edit the following template files,
 *     ```
       /wp-content/plugins/all-in-one-video-gallery/public/templates/video-thumbnail.php
       /wp-content/plugins/all-in-one-video-gallery/public/templates/video-thumbnail-image-left.php
       /wp-content/plugins/all-in-one-video-gallery/public/templates/single-video.php
       ```
   
 * You may see that our template files use “get_the_terms” function from WordPress
   to get the categories. Simply follow the tutorial here [https://www.yukei.net/2016/04/use-get_the_terms-instead-of-wp_get_object_terms/](https://www.yukei.net/2016/04/use-get_the_terms-instead-of-wp_get_object_terms/)
   that provides the exact steps to change the ordering of the categories.
 * Hope, this helped you!
    -  This reply was modified 6 years, 9 months ago by [wpvideogallery](https://wordpress.org/support/users/wpvideogallery/).
 *  Thread Starter [maytro](https://wordpress.org/support/users/maytro/)
 * (@maytro)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/order-category-by-count/#post-11823267)
 * Hi,
    Sorry for my messages but there is a problem with the function that you 
   provide.
 * This is my code now. Do you recognize my error?
 *     ```
       // using get_the_terms()
        $categories = get_the_terms( get_the_ID(), 'aiovg_categories' );
       // $popular_terms will be ordered alphabetically, so let's order by count
       $categories = usort( $categories, function( $a, $b ){
           if ( $a->count < $b->count ) {
               return 1;
           }
           if ( $a->count > $b->count ) {
               return -1;
           }
           return 0;
   
       } );  
       // we only need slugs, so...
       $categories = wp_list_pluck( $categories, 'names' );
   
                   if ( ! empty( $categories ) ) {
                       $meta = array();
                       foreach ( $categories as $category ) {
                           $category_url = aiovg_get_category_page_url( $category );
                           $meta[]       = sprintf( '<a  href="%s" class="aiovg-link-category">%s</a>', esc_url( $category_url ), esc_html( $category->name ) );
                       }
   
   
   
       		   printf( '<div class="aiovg-category"><span class="aiovg-icon-folder-open"></span> %s</div>', implode( ', ', $meta ) );
   
   
   
                   }
       ```
   
 * I got these errors:
    1. Warning: Invalid argument supplied for foreach() in /
   home/media/public_html/materials/wp-includes/class-wp-list-util.php on line 148
 * 2.
 * Warning: Invalid argument supplied for foreach() in /home/media/public_html/materials/
   wp-content/plugins/all-in-one-video-gallery-premium/public/templates/video-thumbnail.
   php on line 78
 *  Plugin Contributor [wpvideogallery](https://wordpress.org/support/users/wpvideogallery/)
 * (@wpvideogallery)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/order-category-by-count/#post-11823555)
 * Kindly remove all your code and simply replace the following line,
 * `$categories = get_the_terms( get_the_ID(), 'aiovg_categories' );`
 * as
 *     ```
       $categories = wp_get_object_terms( get_the_ID(), 'aiovg_categories', array( 
       'orderby' => 'count',
       'order'   => 'DESC'
       ));
       ```
   
 * Hope, this solved your issue!
    -  This reply was modified 6 years, 9 months ago by [wpvideogallery](https://wordpress.org/support/users/wpvideogallery/).
 *  Thread Starter [maytro](https://wordpress.org/support/users/maytro/)
 * (@maytro)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/order-category-by-count/#post-11823618)
 * Now it’s work properly!
    I really appreciate the help.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Order category by count’ is closed to new replies.

 * ![](https://ps.w.org/all-in-one-video-gallery/assets/icon-256x256.png?rev=3078876)
 * [All-in-One Video Gallery](https://wordpress.org/plugins/all-in-one-video-gallery/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/all-in-one-video-gallery/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/all-in-one-video-gallery/)
 * [Active Topics](https://wordpress.org/support/plugin/all-in-one-video-gallery/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/all-in-one-video-gallery/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/all-in-one-video-gallery/reviews/)

 * 6 replies
 * 2 participants
 * Last reply from: [maytro](https://wordpress.org/support/users/maytro/)
 * Last activity: [6 years, 9 months ago](https://wordpress.org/support/topic/order-category-by-count/#post-11823618)
 * Status: resolved