WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Jan 22 2013

How to orderby include Argument for get_terms()

WordPress 3.5 came out with an awesome feature of ordering by post__in (Codex). So when I got to my current issue, I noticed that I could not use the default orderby options (alphabetical title, ID, etc.), so the code used to create the post__in orderby can also be extended to be used for get_terms, which by default is not available in WordPress core. However, there is a great filter to enable this (see WordPress ticket).

Call get_terms()

So, first, I need to setup the function. In this scenario, I am using WooCommerce, but it could work with any theme, or ecommerce solution. For more information on get_terms() please see the WordPress Codex.

<?php
/**
* Output the homepage categories HTML Markup
*
* @uses wps_category_image() Outputs image HTML Markup
*/
function wps_homepage_categories() {
$args = array(
'orderby' => 'include',
'order' => 'ASC',
'hide_empty' => false,
'include' => array( 28 /* Appearal */, 27 /* Accessories */, 30 /* Outerwear */, 29 /* Hiking */, 22 /* Grooming */, ),
'fields' => 'all',
'pad_counts' => false,
'd2c_home' => true, //optional
);
$product_cats = get_terms( 'product_cat', $args );
foreach ( $product_cats as $cat ) {
wps_category_image( $cat->term_id, $size );
}
}
view raw wps_homepage_categories.php hosted with ❤ by GitHub

In this code, I use wps_category_image() which outputs the WooCommerce category image.

<?php
/**
* Outputs HTML markup for category image for WooCommerce.
*
* @param int $cat_id Category ID.
* @param string $size Image size.
* @param array $attr Image attributes.
* @return string $orderby Modified orderby SQL string.
*/
function wps_category_image( $cat_id, $size, $attr = array() ) {
// get the thumbnail id user the term_id
$thumbnail_id = get_woocommerce_term_meta( $cat_id, 'thumbnail_id', true );
// get the image URL
$image = wp_get_attachment_url( $thumbnail_id );
// print the IMG HTML
echo wp_get_attachment_image( $thumbnail_id, $size, false, $attr );
}
view raw wps_category_image.php hosted with ❤ by GitHub

Filter get_terms_orderby

Next, is the really cool part. You want to filter the get_terms_orderby SQL.

<?php
add_filter( 'get_terms_orderby', 'wps_get_terms_orderby', 10, 2 );
/**
* Modifies the get_terms_orderby argument if orderby == include
*
* @param string $orderby Default orderby SQL string.
* @param array $args get_terms( $taxonomy, $args ) arg.
* @return string $orderby Modified orderby SQL string.
*/
function wps_get_terms_orderby( $orderby, $args ) {
if ( isset( $args['orderby'] ) && 'include' == $args['orderby'] ) {
$include = implode(',', array_map( 'absint', $args['include'] ));
$orderby = "FIELD( t.term_id, $include )";
}
return $orderby;
}
view raw wps_get_terms_orderby.php hosted with ❤ by GitHub

First, you need to check to see if include is the preferred orderby argument.

Second, you want to sanitize it with absint().

Third, set the order to the FIELD include (see here for more information).

Written by Travis Smith · Categorized: WordPress

StudioPress Premium WordPress Themes     WP Engine Managed WordPress Hosting

What can I do for you!?

Custom Development

We develop plugins by determining both business/functional and technical requirements, following WordPress development best practices, and using agile methodology to ensure you get the best solution.

Consulting

Have questions? Need a reliable developer to consult? Please contact us today!

Customized Theme

We can customize your theme or child theme, or create a child theme for you based on your needs while enhancing the performance of every individual attribute.

Customized Plugin

We can customize your plugins, extend plugins (e.g., Gravity Forms, Jetpack, Soliloquy) based on your needs ensuring security, performance, and positive business impact.

Contact Us

About Travis Smith

As a WordPress enthusiast, developer, and speaker, Travis writes about what he learns in WordPress trying to help other WordPress travelers, beginners and enthusiasts with tutorials, explanations, & demonstrations.

Comments

  1. Tomasz says

    April 5, 2013 at 2:31 am

    Works great! Thanks

    Reply
  2. Eric Holmes says

    July 31, 2013 at 10:18 am

    Great post, Travis. You should submit this to trac for ‘orderby’=>’include’ support!

    Reply
  3. Joe Buckle says

    May 20, 2014 at 6:53 am

    What a life saver! Thanks

    Reply
  4. Mizzinc says

    June 25, 2014 at 5:07 am

    Awesome. Thank you.

    Reply
  5. Maks Sherstobitow says

    June 27, 2014 at 10:49 am

    Thank you, Travis! It helps a lot!

    Reply
  6. Joel Worsham says

    August 26, 2014 at 1:04 pm

    This doesn’t seem to be working for me… Has core changed in a way that doesn’t allow this?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2025 � WP Smith on Genesis on Genesis Framework � WordPress � Log in