Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Manually weighted taxonomy terms
<?php
// Use this plugin https://wordpress.org/plugins/i-order-terms/ which will let you drag/drop to re-order terms
// get_terms() will automatically order the terms
// get_the_terms() needs to be sorted afterwards. Example below.
function object_to_array( $object ) {
if ( is_array( $object ) OR is_object( $object ) ){
$result = array();
foreach( $object as $key => $value ){
$result[$key] = object_to_array( $value );
}
return $result;
}
return $object;
}
$terms = get_the_terms( get_the_ID(), 'some_taxonomy' );
if ( $terms && ! is_wp_error( $terms ) ) :
// Deep convert to array
$terms = object_to_array( $terms );
// Order by custom_order tag
usort( $terms, function($a, $b) {
return $a['custom_order'] - $b['custom_order'];
});
foreach( $terms as $term ) {
echo $option['name'];
}
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.