Skip Navigation

[Resuelto] Views sorted by 2 custom fields

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

Problem:

The issue here is that the user wanted to sort her view by multiple custom fields.

Solution:

This can be done by using the custom hook below.

add_filter('wpv_filter_query', 'add_custom_fields_to_view_query', 99, 3);
function add_custom_fields_to_view_query($query_args, $view_settings, $view_id ) {
        if ( $view_id == 16257 ){
         
  
       $meta_query = array();
       $meta_query[] = array('key' => 'acf_directory_sort_order');
       $meta_query[] = array('key' => 'acf_shipping_city');
       if ( !isset($query_args['meta_query']) ){
            $query_args['meta_query'] = array();
       }
       $query_args['meta_query'] = array_merge($meta_query, $query_args['meta_query']);
       add_filter('posts_orderby', 'custom_order');
   }
  
   return $query_args;
}
function custom_order($orderby) {
   global $wpdb;
   return $wpdb->postmeta.'.meta_value ASC, mt1.meta_value ASC';
}

Where you simply need to chance the names of the custom fields in the code that you want to sort by.

This support ticket is created hace 6 años, 9 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

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)

Etiquetado: 

Este tema contiene 25 respuestas, tiene 2 mensajes.

Última actualización por Pat hace 6 años, 8 meses.

Asistido por: Shane.

Autor
Mensajes
#620886

Shane
Supporter

Idiomas: Inglés (English )

Zona horaria: America/Jamaica (GMT-05:00)

Hi Pat,

Whats the url for the site so that I can log in.

Thanks,
Shane

#620927

Pat

OOoppps,

I have forgotten this !

Here is the url : enlace oculto
and for the admin : enlace oculto

Regards
Pat

#620931

Shane
Supporter

Idiomas: Inglés (English )

Zona horaria: America/Jamaica (GMT-05:00)

Hi Pat,

Thanks I tried logging in but the credentials don't seem to work.

Could you try them on your end and let me know if it works for you

Thanks,
Shane

#620936

Pat

Hi Shane,
I have updated the password with the value I gave you before and its now working.
Sorry for that.

Regards
Pat

#621454

Shane
Supporter

Idiomas: Inglés (English )

Zona horaria: America/Jamaica (GMT-05:00)

Hi Pat,

Thanks after some deep researching I was able to find this with a help of a colleague.
https://toolset.com/forums/topic/custom-order-by-with-2-custom-fields/#post-387775

This should be able to resolve this for you.

Thanks,
Shane

#621500

Pat

Hi Shake
Thanks for the info
Did you change something in the site as I'm getting an error in the AAA page
Regards
Pat

#621514

Shane
Supporter

Idiomas: Inglés (English )

Zona horaria: America/Jamaica (GMT-05:00)

Hi Pat,

I removed all the changes that were made to the functions.php file.

Could you let me know the error that you are getting?

Thanks,
Shane

#621549

Pat

Hi Shane
here is the error code

Fatal error: Cannot use object of type WP_Query as array in my_site.com/wp-content/plugins/wp-views/embedded/inc/wpv-filter-order-by-embedded.php on line 291

Regards
Pat

#621554

Shane
Supporter

Idiomas: Inglés (English )

Zona horaria: America/Jamaica (GMT-05:00)

Hi Pat,

Please try again it should now be working.

Thanks,
Shane

#622079

Pat

Hi Shane,

I just tried with the proposed solution and this is still not working. I'm getting the same result than with the previews code (meaning, products are not sorted with the desired custom fields).
Can you have a look please and tell me if something is wrong?

Regards
Pat

#622470

Pat

Hi Shane,

I've found what was the issue. In the function :

function custom_order($orderby) {
global $wpdb;
return $wpdb->postmeta.'.meta_value ASC, mt1.meta_value ASC, mt2.meta_value ASC';
}

I was not defining the ASC in the parameters :
return $wpdb->postmeta.'.meta_value, mt1.meta_value, mt2.meta_value';

And so, the sorting was not done !
Sorry for that and thanks for your support

Regards
Pat