Skip Navigation

[Resuelto] sorting by api using wp_query

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

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 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.

Etiquetado: ,

This topic contains 2 respuestas, has 2 mensajes.

Last updated by mohammadD hace 3 años.

Assigned support staff: Minesh.

Autor
Mensajes
#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: Inglés (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