How do I get taxonomy terms by ID in a specific order. Below is my code and I thought if I passed get_terms
the arguments to with ID and post_in but I am not seeing the results I thought I should.
$args_terms = array(
'post_in' => array(47, 48, 49, 46 , 50, 5),
'orderby' => 'post_in',
'parent' => '0'
);
$custom_post_type="menu-food";
$taxonomy = 'menu-food-categories';
$taxonomy_terms = get_terms( $taxonomy, $args_terms);
get_terms
does not support a post__in
argument and I don’t see any other argument that will let you force an order. You can, however, accomplish this with one fof at least two filters:
function gto_forced_order($orderby) {
$orderby = "FIELD(t.term_id,5,1,4)";
return $orderby;
}
add_filter('get_terms_orderby','gto_forced_order');
$terms = get_terms('category');
var_dump($terms);
Or…
function gto_forced_order($pieces) {
$pieces['orderby'] = "ORDER BY FIELD(t.term_id,5,1,4)";
return $pieces;
}
add_filter('terms_clauses','gto_forced_order');
That 5,1,4
are the term IDs. The query will sort in whatever is given there.
Those filters will both work globally, changing every call to get_terms
, but by using the other arguments the filters offer you can control where the filter runs.
function gto_forced_order($pieces,$taxonomies,$args) {
if (in_array('category',$taxonomies)) {
$pieces['orderby'] = "ORDER BY FIELD(t.term_id,5,1,4)";
}
return $pieces;
}
add_filter('terms_clauses','gto_forced_order',1,3);
If you add var_dump($pieces,$taxonomies,$args)
to the top of that callback you can see what you have to work with (though it will make a mess– debugging only).
It is similar with get_terms_orderby
but the parameters are a bit different.