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?
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
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?
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
Hi Lou!
Yes, that worked. 🙂
All is working fine.
My issue is resolved now. Thank you!
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?
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