Home › Toolset Professional Support › [Resolved] Filters in an archive not working
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 |
---|---|---|---|---|---|---|
- | 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)
This topic contains 0 replies, has 1 voice.
Last updated by Minesh 1 week, 1 day ago.
Assisted by: Minesh.
This is a copy of this thread, as it got closed while I was ill and didn't reply for a while. Sorry! Minesh was helping me before. https://toolset.com/forums/topic/filters-in-an-archive-not-working/
I have created a View which displays and allows users to filter results on my website. All filters in the view work well. However, the custom field filters do not work in a parallel WP archive. The two should be pretty much identical, allowing users to seamlessly go to the Search page (View) and then click on any category, keyword, etc., and still filter results in the same way.
hidden link - you can see the View here, click on any taxonomy to see the archive.
Archive: FilterArchives
View: FilterSearch
Taxonomy filters:
- Topics
- Traditions
- Languages
Custom fields filters:
- Publication Year
- Medium
- Difficulty
- Recommended Use
I need all those filters to be available on the following taxonomy archives
- Topics
- Traditions
- Languages
- Figures
- Keywords
I am not using different post types, so only apply to posts.
I am aware that the taxonomy filter for an archive that's currently displayed will not be available. That's fine, as long as the other filters work.
Can you please help me work out what's up with those filters in the Archive? Thanks!
Hello. Thank you for contacting the Toolset support.
Normally - you can not build custom search with taxonomy archive. The custom search is supported with post type archive only.
However - I've added the following code to "Custom Code" section offered by Toolset with code snippet namely "toolset-custom-code"
=> hidden link
function func_adjust_archive_filters__multiselect_multi_options($query) { $tax_archives = array('entry_topic','entry_keywords','entry_language','entry_tradition'); if (!$query->is_admin and is_tax($tax_archives)) { if(isset($_GET['wpv-wpcf-difficulty']) and !empty($_GET['wpv-wpcf-difficulty']) ){ $selected_options = $_GET['wpv-wpcf-difficulty']; $difficulty_relation = array('relation'=>"OR"); foreach($selected_options as $p=>$q): $difficulty_meta_args[] = array('key'=>'wpcf-difficulty', 'value'=>$q, 'type'=>'CHAR', 'compare'=>'LIKE'); endforeach; $difficulty_final_args = array_merge($difficulty_relation,$difficulty_meta_args); $query->query_vars['meta_query'][] = $difficulty_final_args; } if(isset($_GET['wpv-wpcf-use']) and !empty($_GET['wpv-wpcf-use']) ){ $selected_options = $_GET['wpv-wpcf-use']; $recomonded_use_relation = array('relation'=>"OR"); foreach($selected_options as $p=>$q): $recomonded_use_meta_args[] = array('key'=>'wpcf-use', 'value'=>$q, 'type'=>'CHAR', 'compare'=>'LIKE'); endforeach; $recomonded_use_final_args = array_merge($recomonded_use_relation,$recomonded_use_meta_args); $query->query_vars['meta_query'][] = $recomonded_use_final_args; } } return $query; } add_action('pre_get_posts', 'func_adjust_archive_filters__multiselect_multi_options',99,1);
Can you please confirm it works as expected.
Thank you for looking into this! It seems that the Use and Difficulty filters work now, but the Medium and Date don't. Could the same solution be applied to them?
We can offer you support and guide you in the right direction and share inputs that what filters you can use and where or how. We do not code the whole funcationality for you. I already help you with how to adjust the filters while using view and now with this ticket with archive.
You can take initiative and try to add the missing filters and adjust the code accordingly. If you require more help with such custom requirement, you are always welcome to contact any of our certified partners:
- https://toolset.com/contractors/
Thanks for taking the time. I can't afford a contractor as this is an academic, non-profit project. I just used ChatGPT 😉 I managed to get the Medium filter going and it's all pretty much almost there! The Year one is tricky and I worry that I'm almost there but get something wrong. Maybe the labels of the 'year-from' and 'year-to' are wrong? I'd be most obliged for any suggestions.
$year_meta_query = array('relation' => 'AND'); // Ensures both conditions apply
if (isset($_GET['year_from']) && !empty($_GET['year_from'])) {
$year_meta_query[] = array(
'key' => 'wpcf-year',
'value' => $_GET['year_from'],
'type' => 'NUMERIC',
'compare' => '>='
);
}
if (isset($_GET['year_to']) && !empty($_GET['year_to'])) {
$year_meta_query[] = array(
'key' => 'wpcf-year',
'value' => $_GET['year_to'],
'type' => 'NUMERIC',
'compare' => '<='
);
}
// Only add the year query if at least one condition is set
if (count($year_meta_query) > 1) {
$query->query_vars['meta_query'][] = $year_meta_query;
}
Somehow I can not able to access the site. It says message "This site can’t be reached".
Can you please make it working and send me access details.
What exactly you want with year filter?
I have set the next reply to private which means only you and I have access to it.
Unfortunately - the admin access details you shared with me is not working at this end.
Can you please send me working admin access details.
*** 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.
Thank you I can login now.
But I cant find the custom code I added that I shared with my previous reply also where you added your code?
- https://toolset.com/forums/topic/filters-in-an-archive-not-working-2/#post-2800277
Also I do not able to edit the custom code snippet.
Ah, the 'this site can't be reached' turned out to be an issue I had to fix by calling in a backup and it looks like this was from before you made the changes. I now added the code you wrote + the medium part back into the toolset-custom-code.php - I hope it's ok.
There are so many moving parts in this, thanks for keeping track.
With your restore process there must be somthing is missing as when I click on the "Search" button it shows critical error. That was not the issue when I shared the solution before.
Can you please check what is the error.
It's weird, it was only breaking on the custom field filters, not the taxonomy filters. I wanted to test if it's because of the custom code, so I deactivated it. It started working. Now I activated it and it still works. Switching it off and on again works? Odd... Anyway, it should work, I hope.
Can you please check now.
- I've adjusted the "pre_get_posts" hook code to "Custom Code" section offered by Toolset with code snippet namely "toolset-custom-code" as given under:
=> hidden link
function func_adjust_archive_filters__multiselect_multi_options($query) { $tax_archives = array('entry_topic','entry_keywords','entry_language','entry_tradition'); if (!$query->is_admin and is_tax($tax_archives)) { if(isset($_GET['wpv-wpcf-difficulty']) and !empty($_GET['wpv-wpcf-difficulty']) ){ $selected_options = $_GET['wpv-wpcf-difficulty']; $difficulty_relation = array('relation'=>"OR"); foreach($selected_options as $p=>$q): $difficulty_meta_args[] = array('key'=>'wpcf-difficulty', 'value'=>$q, 'type'=>'CHAR', 'compare'=>'LIKE'); endforeach; $difficulty_final_args = array_merge($difficulty_relation,$difficulty_meta_args); $query->query_vars['meta_query'][] = $difficulty_final_args; } if(isset($_GET['wpv-wpcf-use']) and !empty($_GET['wpv-wpcf-use']) ){ $selected_options = $_GET['wpv-wpcf-use']; $recomonded_use_relation = array('relation'=>"OR"); foreach($selected_options as $p=>$q): $recomonded_use_meta_args[] = array('key'=>'wpcf-use', 'value'=>$q, 'type'=>'CHAR', 'compare'=>'LIKE'); endforeach; $recomonded_use_final_args = array_merge($recomonded_use_relation,$recomonded_use_meta_args); $query->query_vars['meta_query'][] = $recomonded_use_final_args; } if(isset($_GET['wpv-wpcf-medium']) and !empty($_GET['wpv-wpcf-medium']) ){ $selected_options = $_GET['wpv-wpcf-medium']; $medium_relation = array('relation'=>"OR"); foreach($selected_options as $p=>$q): $medium_meta_args[] = array('key'=>'wpcf-medium', 'value'=>$q, 'type'=>'CHAR', 'compare'=>'LIKE'); endforeach; $medium_final_args = array_merge($medium_relation,$medium_meta_args); $query->query_vars['meta_query'][] = $medium_final_args; } $year_meta_query = $year_meta_min = $year_meta_max = array(); if (isset($_GET['wpv-wpcf-year_min']) && !empty($_GET['wpv-wpcf-year_min']) ) { $year_meta_min = array( 'key' => 'wpcf-year', 'value' => $_GET['wpv-wpcf-year_min'], 'type' => 'NUMERIC', 'compare' => '>=' ); } if (isset($_GET['wpv-wpcf-year_max']) && !empty($_GET['wpv-wpcf-year_max']) ) { $year_meta_max = array( 'key' => 'wpcf-year', 'value' => $_GET['wpv-wpcf-year_max'], 'type' => 'NUMERIC', 'compare' => '<=' ); } if(!empty($year_meta_min) and !empty($year_meta_max)){ $year_meta_query = array('relation'=>'AND', $year_meta_min, $year_meta_max ); }else if(!empty($year_meta_min)){ $year_meta_query = $year_meta_min; }else if(!empty($year_meta_max)){ $year_meta_query = $year_meta_max; } if (!empty($year_meta_query)) { $query->query_vars['meta_query'][] = $year_meta_query; } } return $query; } add_action('pre_get_posts', 'func_adjust_archive_filters__multiselect_multi_options',99,1);
Can you plese confirm now year filter is also working as expected.