Skip Navigation

[Resolved] I can't get orderby name working

This support ticket is created 3 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
- 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 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Shane 3 years, 6 months ago.

Assisted by: Shane.

Author
Posts
#2081603

Tell us what you are trying to do?
I want my custom posts to be ordered by slug

Is there any documentation that you are following?
I have followed the instructions from several similar support forum posts, and I have included the following code in my functions.php:

function swm_custom_post_order_sort( $query ) {
	if ( $query->is_main_query() ) {
		$query->set( 'posts_per_page', -1 );
		$query->set( 'orderby', array( 'name' => 'ASC' ) );
	}
}
add_action( 'pre_get_posts', 'swm_custom_post_order_sort', 1000 );


add_filter( 'wpv_filter_query', 'order_by_slug_func', 999, 3 );
function order_by_slug_func( $query_args, $view_settings, $view_id ) {
	if ( in_array( $view_id, array( 239405 ) ) ) {
		$query_args['orderby']        = 'name';
		$query_args['order']          = 'ASC';
		$query_args['posts_per_page'] = -1;
	}
	return $query_args;
}

What is the link to your site?
hidden link

#2082061

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Amanda,

Thank you for getting in touch.

Are you ordering a list of posts from a View ?

Can you provide some clarity on the context so that I can better assist?

Thanks,
Shane

#2095483

Yes, I have a view of a list of post, using the classic editor, and the posts are sorted by title. It also has a custom search.

#2095713

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Screenshot 2021-06-22 at 9.10.10 AM.png

Hi Amanda,

In a case like this then you won't need to write a custom hook to modify the sort order.

There should be an ordering option on the edit screen of your view. Have a look at my screenshot

You are able to set the ordering of the view through the user interface. The use of custom hooks aren't required for this.

Please let me know if this helps.

Thanks,
Shane