Skip Navigation

[Resolved] I need to order by slug, not title

This thread is resolved. Here is a description of the problem and solution.

Problem:
The customer needs to order their Custom Post Types (CPTs) by slug instead of title. For example, a film titled "The Return" with a slug of "return" should be listed under the 'R' section in their film archive. The View options do not support ordering by slug, and an attempt to use a pre_get_posts() filter to achieve this has failed.
Solution:

We suggested using the wpv_filter_query filter to change the orderby parameter. The provided custom code should be added to the theme's functions.php file. The customer needs to replace 1000 with their post view's ID and your_custom_field_slug_here with the custom field slug (name) of the Film.
Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

This topic contains 1 reply, has 2 voices.

Last updated by Mateus Getulio 7 months, 2 weeks ago.

Assisted by: Mateus Getulio.

Author
Posts
#2699168

I need to order my CPTs by slug, not title. For example, I have a film called "The Return", with a slug of "return". This film needs to be in the R section of my films, but ordering by slug (or name) is not an option in the View options.

I have tried adding a pre_get_posts() filter, but this is not working:

`
add_action( 'pre_get_posts', 'custom_archive_order' );
function custom_archive_order( $query ) {

if ( ! is_admin() && $query->is_post_type_archive( 'film' ) ) {
// Not a query for an admin page.
// It's the main query for a front end page of your site.
$query->set('orderby', 'name');
$query->set('order', 'ASC');
}

}
`

#2699194

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello there,

It is possible to achieve it with some custom code, you can use wpv_filter_query to change the orderby parameter, for example:

Add the code bellow into your theme's functions.php file:

add_filter( 'wpv_filter_query', function($query, $setting, $view_id){
	if(in_array($view_id,array(1000))){
		$query['meta_key'] = 'wpcf-' . 'your_custom_field_slug_here';
		$query['orderby'] = array('meta_value_num' => 'ASC', 'title' => 'ASC');
	}
	return $query;
}, 999, 3);

Please replace 1000 with your post view's ID and replace your_custom_field_slug_here with the custom field slug(name?) of the Film.

More help:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters