Skip Navigation

[Resuelto] Nested views, second sorting on custom field

This support ticket is created hace 1 año, 10 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
- 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)

Este tema contiene 9 respuestas, tiene 3 mensajes.

Última actualización por Simon hace 1 año, 10 meses.

Asistido por: Minesh.

Autor
Mensajes
#2536127

I wish to sort a view (legacy views) on two custom fields.

The secondary sorting does not allow custom fields and I thought I maybe could use two views that are nested?

1.0: View 1 sort by custom field A for post type A.
1.1: Take all post ids that is matching from 1.0 and send to View 2.
2.0: View 2 sort on custom field B for post type A with all matching ids from 1.1.

* Is this possible to collect all post ids and send in a batch to View 2?
* Is it possible to register/add a custom field for the standard secondary posting?

#2536321

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

Hi there

The UI doesn't let you set up secondary sorting via a custom field, but you could impose that via the API, namely by using the wpv_filer_query hook to modify the query arguments to suit your needs.

That would be based on the possible arguments for the underlying WP_Query, and in particular you would need to model your code on the example under "‘orderby’ with multiple ‘meta_key’s" at https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

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

(Note that Types custom fields are stored in wp_postmeta with a 'wpcf-' prefix, so if you have a "priority" custom field, it would be stored in wp_postmeta with a key of 'wpcf-priority'.)

#2537065

Thank you!

I wrote the following code and I can see the orderby array in WPV debug window but the result is not changed.
Do I need to change $view_settings too?

add_filter('wpv_filter_query', 'bmg_custom_sort', 101, 3);

function bmg_custom_sort($query_args, $view_settings, $view_id)
{
    if( 5171 == $view_id ){

        $query_args['orderby'] = array(
        		'wpcf-vin-region' => 'ASC',
        		'wpcf-vin-producentens-namn' => 'ASC',
        	);

        unset($query_args['wpv_orderby']);
        unset($query_args['wpv_order']);

 	}

 	return $query_args;
}

Debug information:

WP_Query Object
(
    [query] => Array
        (
            [post_type] => Array
                (
                    [0] => viner
                )

            [paged] => 1
            [suppress_filters] => 
            [ignore_sticky_posts] => 1
            [ep_integrate] => 
            [posts_per_page] => -1
            [wpv_original_limit] => -1
            [wpv_original_offset] => 0
            [wpv_original_posts_per_page] => -1
            [meta_query] => Array
                (
                    [0] => Array
                        (
                            [key] => wpcf-vin-land
                            [value] => Frankrike
                            [type] => CHAR
                            [compare] => =
                        )

                    [relation] => AND
                )

            [post_status] => Array
                (
                    [0] => publish
                    [1] => private
                )

            [orderby] => Array
                (
                    [wpcf-vin-region] => ASC
                    [wpcf-vin-producentens-namn] => ASC
                )

            [order] => ASC
            [wpv_query] => 1
        )
#2537779

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

I will require bit more information about your structure.

1.0: View 1 sort by custom field A for post type A.
==>
Lets say this is your view 1.

1.1: Take all post ids that is matching from 1.0 and send to View 2.
==>
The View 2 is another view where you are getting the post IDs as a source to pass to the view 1 and those found post IDs posts will be displayed - correct?

2.0: View 2 sort on custom field B for post type A with all matching ids from 1.1.
==>
This is where I've bit confusion.

If you can setup a test install for me where I can review your structure and where you wanted to display what and what is your expected result.

If you want I can even setup a sandbox site for you where you can setup your structure with content and then I'll be able to guide you in the right direction.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2537931

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Have you added any views filter like "wpv_fitler_query" hook - if yes, where? I'm not able to see the theme files.

#2537941

I have deleted the wpv_fitler_query in the functions.php file in this development setup.
The code I used was in the earlier reply (#2537065) from me.

#2537951

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

That is what I shared with my previous reply that I do not have capabilities to access your theme files and you added the filter code to your theme's funcitons.php file.

Can you please remove that code or grant me access so that I can access the theme files?

I have set the next reply to private which means only you and I have access to it.

#2537967

You can now write to functions.php through the Theme File Editor at /wp-admin/theme-editor.php or use the plugin Code Snippets at /wp-admin/admin.php?page=snippets . The previous code in functions.php is removed by me in order to get a clean installation.

Due to security reasons it is not possible to connect over FTP/S or SFTP to the server.

#2538657

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Could you please check now: enlace oculto

I've added the following code to "Custom Code" section offered by Toolset:
=> enlace oculto

function func_custom_sort($query_args, $views_settings, $view_id) {
   
    if ($view_id == 5171) {
         
      $query_args['meta_query'][] = array(
        'relation' => 'AND',
        'region_clause' => array(
            'key' => 'wpcf-vin-region',
           
        ),
         'producentens_clause' => array(
            'key' => 'wpcf-vin-producentens-namn',
           
        ),
    );
    $query_args['orderby'] = array(
       'region_clause'=> 'ASC',
       'producentens_clause' => 'ASC');

      
    }
   
    return $query_args;
}
add_filter( 'wpv_filter_query', 'func_custom_sort', 101, 3);

More info:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

#2538681

Thank you very much for the help!

This is exactly what we were looking for.