Saltar navegación

[Resuelto] Show only search results with the search string at the beginning

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

Problem:
How to add a custom search for some field where the field value starts with the search term (rather than the normal behaviour where the search term can appear anywhere inside the field value).

Solution:
It is not supported within the UI, but you can use the Views API to customise the query arguments to take advantage of the ability to use regular expressions in meta queries.

Following is an example of such code, which needs editing for the ID(s) of the View(s) it should apply to, and the custom fields which should use this approach:

function tssupp_filter_query($view_args, $view_settings, $view_id)
{
 
    $views = array( 167 ); // comma-separated array of View IDs to modify
 
    $fields = array( 'wpcf-searchable' ); // comma-separated array of fields to modify filter of, including wpcf- prefix
 
    if ( in_array($view_id, $views ) && isset( $view_args['meta_query'] ) ) {
 
        $meta_queries = $view_args['meta_query'];
 
        foreach ($meta_queries as $key => $meta_query) {
 
            if ( in_array( $meta_query['key'], $fields ) ){
                $view_args['meta_query'][$key]['value'] = '^'.$meta_query['value'];
                $view_args['meta_query'][$key]['compare'] = 'REGEXP';
            }
        }
    }
    return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_filter_query', 101, 3);

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters

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

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Este tema contiene 2 respuestas, tiene 2 mensajes.

Última actualización por Alexander hace 5 años, 6 meses.

Asistido por: Nigel.

Autor
Mensajes
#1301303

Tell us what you are trying to do? I am trying to display only search/ filter results for the field "Postleitzahl" (zip code) where the results START with the number(s) the user enters. Instead I see all results containing the searched number, even if it's in the middle of the zip code.

Is there any documentation that you are following? I looked through the Toolset documentation and couldn't find a way to get this to work the way I expected it to. I thought regex might help but couldn't find a way to use them.

Is there a similar example that we can see? Entering "81" in the field "Postleitzahl" displays more results than expected. Not only those in the Munich area that start with "81" but also those with the number somewhere in the middle.

What is the link to your site? enlace oculto

#1301563

Nigel
Supporter

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

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

So the documentation I had referred to was out-of-date, and the current documentation reports that you *can* use regular expressions in meta queries: https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters

But according to that it is available for simple meta queries "REGEXP" is an option for the meta_compare parameter, but not for complex meta queries (it's not available for the compare parameter).

But I tested it and found that it works anyway.

So here is a solution you can use which will modify the query generated by Views using the wpv_filter_query hook.

You'll need to add the following code to your site (e.g. as a code snippet at Toolset > Settings > Custom Code):

function tssupp_filter_query($view_args, $view_settings, $view_id)
{

    $views = array( 167 ); // comma-separated array of View IDs to modify

    $fields = array( 'wpcf-searchable' ); // comma-separated array of fields to modify filter of, including wpcf- prefix

    if ( in_array($view_id, $views ) && isset( $view_args['meta_query'] ) ) {

        $meta_queries = $view_args['meta_query'];

        foreach ($meta_queries as $key => $meta_query) {

            if ( in_array( $meta_query['key'], $fields ) ){
                $view_args['meta_query'][$key]['value'] = '^'.$meta_query['value'];
                $view_args['meta_query'][$key]['compare'] = 'REGEXP';
            }
        }
    }
    return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_filter_query', 101, 3);

You will need to edit this for the View IDs it should be applied to, and which fields should be modified to work with the 'starts with...' regular expression. (And note that you should include the 'wpcf-' prefix which Types uses when storing custom fields in wp_postmeta.)

#1302801

My issue is resolved now. Thank you!