I am trying to: order my post by custom category (alphabetical)
I expected to see: order by category (custom cat of my custom post)
Instead, I got: no such item in the dropdown. (view screenshot)
is it possible? How?
Thanks
Noman
Supporter
Languages:
English (English )
Timezone:
Asia/Karachi (GMT+05:00)
Hi Benoît,
Thank you for contacting Toolset Support. We do not have any builtin option for this, but you can get this order using some custom code like below.
Please add this code in your theme’s or child theme’s functions.php file:
function taxonomy_orderby( $orderby, $wp_query ) {
global $wpdb;
if ( isset( $wp_query->query['orderby'] ) ) {
$orderby = "(
SELECT GROUP_CONCAT(name ORDER BY name ASC)
FROM $wpdb->term_relationships
INNER JOIN $wpdb->term_taxonomy USING (term_taxonomy_id)
INNER JOIN $wpdb->terms USING (term_id)
WHERE $wpdb->posts.ID = object_id
AND taxonomy = ''
GROUP BY object_id
) ";
$orderby .= ( 'ASC' == strtoupper( $wp_query->get('order') ) ) ? 'ASC' : 'DESC';
}
return $orderby;
}
add_filter( 'posts_orderby', 'taxonomy_orderby', 10, 2 );
More info: https://wordpress.stackexchange.com/questions/14306/using-wp-query-is-it-possible-to-orderby-taxonomy
Thank you