Skip Navigation

[Resolved] order by category

This support ticket is created 5 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 1 reply, has 2 voices.

Last updated by Noman 5 years, 6 months ago.

Assigned support staff: Noman.

Author
Posts
#530780
Edit View_order.png

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

#530916

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