Skip Navigation

[Resolved] sorting by api using wp_query

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

Problem:
How to change sort order using view's API hook

Solution:
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:
=> https://toolset.com/forums/topic/sorting-by-api-using-wp_query/#post-691182

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 6 years 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by mohammadD 6 years ago.

Assisted by: Minesh.

Author
Posts
#691164

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
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
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?

#691182

Minesh
Supporter

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 );

Where:
- Replace 9999 with your original view id.

#691350

tnx

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.