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
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
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.
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
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