Skip Navigation

[Resolved] sorting by api using wp_query

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

How to change sort order using view's API hook

You can use view's hook 'wpv_filter_query' to change sort order.

You can find proposed solution, in this case, with the following reply:

Relevant Documentation:

This support ticket is created 4 years, 5 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.

This topic contains 2 replies, has 2 voices.

Last updated by mohammadD 4 years, 5 months ago.

Assigned support staff: Minesh.


i need sort results and for some reasons i don't want use views sorting controls

well there is a hook for views i'm using: wpv_filter_query
and i believe i can use ordering methods of wp_query
with the $query_args parameter of the hook;

for example i'm doing this in callback_function:
$query_args["orderby"] = "title";
$query_args["order"] = "ASC";
return $query_args;

but it is not in use;

how can i change the orderby and order of a query using API?



Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - could you please try to use following code and try to resolve your issue. The key thing here is that we need to set high priority for this hook.

function custom_ordering( $view_args, $view_settings, $view_id ) {
    if ( $view_id == 9999) { // edit the id
         $view_args['orderby'] = 'title';
        $view_args['order'] = 'ASC';
    return $view_args;
add_filter( 'wpv_filter_query', 'custom_ordering', 101, 3 );

- Replace 9999 with your original view id.