Skip Navigation

[Résolu] Display only Filtered Values

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:

I have a custom search which filters by post ID based on URL query.
Right now for an empty query the view returns all results.

Is there a way to return the [wpv-no-items-found] bit intstead for this particular view?

Solution:

f you pass empty URL parameter "my-url-param" to view, view will output all result.

In your case, you can change it with Views filter hook "", for example add below codes into your theme/functions.php:

add_filter( 'wpv_filter_query', 'show_empty_default_func', 99, 3 );
      
function show_empty_default_func( $query_args, $setting, $view_id ) {
    if($view_id == 12345)
    {
        if( isset($_GET['my-url-param']) && $_GET['my-url-param'] == '' ) 
        {
            $query_args['post__in'] = array(0);
        }
    }
    return $query_args;
}

Please replace 12345 with your view's ID, replace "my-url-param" with your URL parameter

Relevant Documentation:

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

This support ticket is created Il y a 6 années et 8 mois. 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

Marqué : ,

Ce sujet contient 3 réponses, a 2 voix.

Dernière mise à jour par Luo Yang Il y a 6 années et 8 mois.

Assisté par: Luo Yang.

Auteur
Publications
#640630

Hi,

I have a custom search which filters by post ID based on URL query.
Right now for an empty query the view returns all results.

Is there a way to return the [wpv-no-items-found] bit intstead for this particular view?

Thank you

Adrian

#641028

Dear Adrian,

If you pass empty URL parameter "my-url-param" to view, view will output all result.

In your case, you can change it with Views filter hook "", for example add below codes into your theme/functions.php:

add_filter( 'wpv_filter_query', 'show_empty_default_func', 99, 3 );
     
function show_empty_default_func( $query_args, $setting, $view_id ) {
    if($view_id == 12345)
    {
        if( isset($_GET['my-url-param']) && $_GET['my-url-param'] == '' ) 
        {
            $query_args['post__in'] = array(0);
        }
    }
    return $query_args;
}

Please replace 12345 with your view's ID, replace "my-url-param" with your URL parameter

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

#641043

Hi Luo,

Thank you for your suggestion. I will try that out soon.
In the meantime, I was able to add a redirect for that specific slug, which only impacts the default URL. If an ID is passed via URL string, it no longer redirects.

Very primitive, I know but does the trick for now. I will try your solution as well in future versions and I will post back here if I have any issues implementing it.

Thank you

#641373

You are welcome