Skip Navigation

[Résolu] views: need to filter on the same field for the query and for search

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

Problem:

Setup custom field filter by constant value + URL parameter value.

Solution:

When there isn't URL parameter passing to view, it will output all results by default, and it conducts the problem, you can consider the custom codes:

https://toolset.com/forums/topic/views-need-to-filter-on-the-same-field-for-the-query-and-for-search/#post-1586183

Relevant Documentation:

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

This support ticket is created Il y a 4 années et 7 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

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 7 réponses, a 2 voix.

Dernière mise à jour par Jim Il y a 4 années et 7 mois.

Assisté par: Luo Yang.

Auteur
Publications
#1584781

Jim

I have a custom field (a radio element) with four values:
j0, sA, sB, sC

First I want to filter out 'j0' so I only get posts with sA, sB or sC
This will display all the posts with these three values set.

So I creater my filter like this: Specials is een string in sA, sB, sC

Now I would like to use a search filter on the page to be able to toggle between these three type of posts.

So I added the URL-parameter to the query string: Specials is een string in sA, sB, sC, URL_PARAM(spcls)

lien caché

But that does not work...
Once I add that, the posts are no longer filtered on sA, sB, sC.

Any idea how this can be acomplished?

#1584981

Hello,

Since you are using URL parameter in the field filter, so when there isn't URL parameter passing to view, it will output all results by default, and it conducts the problem you mentioned above.

In your case, I suggest you try these:
1) Edit the post view, remove those constant filter: sA, sB, sC, see your screenshot:
lien caché

2) Add those constant filter with wpv_filter_query filter, for example, add below codes into your theme file functions.php:

add_filter('wpv_filter_query', function($query, $settings, $view_id){
	if($view_id == 123){
		$query['meta_query'][] = array(
			'key'	=> 'wpcf-veld-specials',
			'compare'	=> 'IN',
			'value'	=> array('sA', 'sB', 'sC'),
		);
		$query['meta_query']['relation'] = 'OR';
	}
	return $query;
}, 10, 3);

Please replace 123 with your post view's ID.

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

#1585023

Jim

Hi Luo,
thanks, the query filter is working! But the search filter does not...

I left the URL-parameter in the query:
Specials is een string gelijk aan URL_PARAM(spcls)

I created a custom select dropdown to be able to leave
This is my search code:
[wpv-control-postmeta field="wpcf-specials" url_param="spcls" type="select" default_label="Alle specials" source="custom" values="sA,sB,sC" display_values="Special ovv HRR ,Special - niet medisch, Special medisch - Commercieel"]

What did I do wrong here?

#1586183

Please try these:
1) Edit the filter of query as below:
Select items with field:
Specials is a string equal to URL_PARAM(spcls)

2) Edit the PHP codes as below:

add_filter('wpv_filter_query', function($query, $settings, $view_id){
	if($view_id == 123 ){
		if(isset($_GET['spcls']) && !empty($_GET['spcls'])){ // search with spcls URL parameter
			return $query;
		}
		$query['meta_query'][] = array(
			'key'	=> 'wpcf-veld-specials',
			'compare'	=> 'IN',
			'value'	=> array('sA', 'sB', 'sC'),
		);
		//$query['meta_query']['relation'] = 'OR';
	}
	return $query;
}, 10, 3); 

Please replace 123 with your post view's ID, and test again

#1587217

Jim

Hi Lou!

Yes, that worked. 🙂
All is working fine.

My issue is resolved now. Thank you!

#1588547

Jim

Lou, still there?

I ran into a new problem...

I now have two pages with the same kind of view but slightly different, so I copied the code and changed the view id for the other view and thought it would work but it doesn't.
Somehow both functions are read no matter the view id:

//16049 = specials archive
add_filter('wpv_filter_query', function($query, $settings, $view_id){
    if($view_id == 16049 ){
        if(isset($_GET['spcls']) && !empty($_GET['spcls'])){ // search with spcls URL parameter
            return $query;
        }
        $query['meta_query'][] = array(
            'key'   => 'wpcf-specials',
            'compare'   => 'IN',
            'value' => array('sA', 'sB', 'sC'),
        );
        //$query['meta_query']['relation'] = 'OR';
    }
    return $query;
}, 10, 3); 


//16054 = specials archive consumers
add_filter('wpv_filter_query', function($query, $settings, $view_id){
    if($view_id == 16054){
        if(isset($_GET['spcls']) && !empty($_GET['spcls'])){ // search with spcls URL parameter
            return $query;
        }
        $query['meta_query'][] = array(
            'key'   => 'wpcf-specials',
            'compare'   => 'IN',
            'value' => array('sB', 'sC'),
        );
        //$query['meta_query']['relation'] = 'OR';
    }
    return $query;
}, 10, 3); 

Can you spot the problem?

#1589433

Jim

Found the solution by combining the two into one:

add_filter('wpv_filter_query', function($query, $settings, $view_id){
if($view_id == 16049 ){
if(isset($_GET['spcls']) && !empty($_GET['spcls'])){ // search with spcls URL parameter
return $query;
}
$query['meta_query'][] = array(
'key' => 'wpcf-specials',
'compare' => 'IN',
'value' => array('sA', 'sB', 'sC'),
);
}
//$query['meta_query']['relation'] = 'OR';
if($view_id == 16054){
if(isset($_GET['spcls']) && !empty($_GET['spcls'])){ // search with spcls URL parameter
return $query;
}
$query['meta_query'][] = array(
'key' => 'wpcf-specials',
'compare' => 'IN',
'value' => array('sB', 'sC'),
);
//$query['meta_query']['relation'] = 'OR';
}
return $query;
}, 10, 3);

Working 🙂
Bye

#1589435

Jim
add_filter('wpv_filter_query', function($query, $settings, $view_id){
	if($view_id == 16049 ){
        if(isset($_GET['spcls']) && !empty($_GET['spcls'])){ // search with spcls URL parameter
            return $query;
        }
        $query['meta_query'][] = array(
            'key'   => 'wpcf-specials',
            'compare'   => 'IN',
            'value' => array('sA', 'sB', 'sC'),
        );
	}
	//$query['meta_query']['relation'] = 'OR';
	if($view_id == 16054){
	    if(isset($_GET['spcls']) && !empty($_GET['spcls'])){ // search with spcls URL parameter
	        return $query;
	    }
	    $query['meta_query'][] = array(
	        'key'   => 'wpcf-specials',
	        'compare'   => 'IN',
	        'value' => array('sB', 'sC'),
	    );
	    //$query['meta_query']['relation'] = 'OR';
	}
    return $query;
}, 10, 3);