Home › Toolset Professional Support › [Resolved] Add code to search multiple taxonomies
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 – 12:00 | 9:00 – 12:00 | 9:00 – 12:00 | 9:00 – 12:00 | 9:00 – 12:00 | - |
- | 13:00 – 18:00 | 13:00 – 18:00 | 13:00 – 18:00 | 14:00 – 18:00 | 13:00 – 18:00 | - |
Supporter timezone: America/Jamaica (GMT-05:00)
Tagged: Views plugin
This topic contains 19 replies, has 2 voices.
Last updated by davidm-13 4 years, 4 months ago.
Assisted by: Shane.
Hi,
I've used a content template to create the attached page. The select dropdowns on the right contain 3 taxonomy lists, I want to search for the selected taxonomy terms in custom posts, I also want an option to search for a) All taxonomy items selected, or b) Any of the items selected. Please could you give me some sample code or point me to existing code to get me started - if I need to do this in a View how would I insert the code (or View ) into my content template?
Thanks for any help
Hi David,
Thank you for getting in touch.
From what I see you want to give the user the option to operate the filters in an AND or and OR fashion. When the user selects "All taxonomy items selected" then the comparison should be an AND and return the posts that has all those terms selected and for the "Any of the items selected" option this would do an OR comparison where it will return a post with at least one of the terms attached.
Unfortunately this is not something that can be changed on the fly by users as this is a backend setting on the filter itself so some amount of custom code is needed to get this done which would fall outside of our support scope.
Thanks,
Shane
Hi Shane, Thanks for this, can you recommend what would be the best path for me to take to implement this taxonomy query. Are there any plugins I could use? Would Relevanssi do what I need or should I just code the wp_query from scratch. Thanks or any advice.
Hi David,
Looking at it you may need to code the WP_Query from scratch. Given that this would mean changing the comparison logic being used.
You can make use of our views API hook to modify the filter query.
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
Thanks,
Shane
Ok Shane,
I have the below code for the WP_Query:
function dm_tax_filter(){ if(isset($_POST['submit'])) { $relation = 'OR'; if($_POST['all-any'] == 'All') { $relation = 'AND'; } if( !empty( $_POST['wpv-system-state'] ) || !empty( $_POST['wpv-complaint']) || !empty( $_POST['wpv-actions'])) { $args['tax_query'] = array( 'relation' => $relation, array( 'taxonomy' => 'complaint-use', 'field' => 'slug', 'terms' => $_POST['wpv-complaint'] ), array( 'taxonomy' => 'system', 'field' => 'slug', 'terms' => $_POST['wpv-system-state'] ), array( 'taxonomy' => 'action-medicinal', 'field' => 'slug', 'terms' => $_POST['wpv-actions'], ) ); } else { exit('Please select from at least one of the above categories.'); } $query = new WP_Query( $args ); if( $query->have_posts() ) : while( $query->have_posts() ): $query->the_post(); get_template_part('search-results'); endwhile; wp_reset_postdata(); else : echo 'No posts found'; endif; die(); } }
Can you please advise - coding example appreciated - how I can just display the query results using Views or content template.
Thanks
Hi David,
Assuming that this code above works, then you should be able to use it to modify our views query.
//Return only posts from the current author when listing posts of type company: add_filter( 'wpv_filter_query', 'prefix_show_only_current_author', 99,3); function prefix_show_only_current_author( $query_args,$view_setting,$view_id ) { if($view_id == 1111){ if(isset($_POST['submit'])) { $relation = 'OR'; if($_POST['all-any'] == 'All') { $relation = 'AND'; } if( !empty( $_POST['wpv-system-state'] ) || !empty( $_POST['wpv-complaint']) || !empty( $_POST['wpv-actions'])) { $query_args['tax_query'] = array( 'relation' => $relation, array( 'taxonomy' => 'complaint-use', 'field' => 'slug', 'terms' => $_POST['wpv-complaint'] ), array( 'taxonomy' => 'system', 'field' => 'slug', 'terms' => $_POST['wpv-system-state'] ), array( 'taxonomy' => 'action-medicinal', 'field' => 'slug', 'terms' => $_POST['wpv-actions'], ) ); } return $query_args; }
Please try this and let me know if it helps.
Thanks,
Shane
Hi Shane,
Thanks for modifying my code.
I have a content template where I select all the parameters to pass to the View.
Have some questions about how to build this View and include it in the content template
1. In Custom Search Settings I selected
Full page refresh when visitors click on the search button with input values auto-updating
2. In Search and Pagination section do I include the shortcode for the above modified code in a New Filter, how would I format this?
3.What goes in the Loop Editor, and if I want to use the content template that contains this View do I need to fill the Templates for this View section?
4.Do I need to: Disable the wrapping DIV around the View
5.Do I specify the results format in the View Output Editor or in the current content template that contains the View - how do I access the results in the content template?
Sorry for all the questions, but I need to get all this clear now for doing any future development with Toolset, otherwise I'm always just fumbling around.
Thanks
Hi David,
Based on your code this hook will only modify the view query if the user has submitted the form with the relevant fields checked.
1. In Custom Search Settings I selected
Full page refresh when visitors click on the search button with input values auto-updating
This should work perfectly fine since the hook will still pick up the values, given that the parameters that it is checking for are correct.
2. In Search and Pagination section do I include the shortcode for the above modified code in a New Filter, how would I format this?
Given that you search fields are already there that you want to use, then they should already be passing the URL values so unless there is a field in the code that is not on your form then you don't need to add anything else to your search form.
3.What goes in the Loop Editor, and if I want to use the content template that contains this View do I need to fill the Templates for this View section?
In the Loop editor you can use the Loop Wizard to select the data you want to display and how you want to display it.
4.Do I need to: Disable the wrapping DIV around the View
No you do not need to disable this.
5.Do I specify the results format in the View Output Editor or in the current content template that contains the View - how do I access the results in the content template?
You will need to specify the output in the Loop Editor using the Loop Wizard. This will generate a content template for your view.
Please let me know if this clears things up for you.
Thanks,
Shane
Hi Shane, Thanks for answering all my questions.
Now have a couple more.
1. Do I insert the above code into Toolset settings > custom snippets?
2. Line 3 of the code you inserted
if($view_id) == 1111
Do I change this to my View ID?
Thanks
Hi David,
1. Do I insert the above code into Toolset settings > custom snippets?
Yes this is where you add the code, also ensure that you click activate once you've saved it.
Secondly yes you will need to change 1111 to the ID of your view that you want to modify the query for.
Thanks,
Shane
Is the attached code snippet setup correct?
I see that if I specify Run mode as: On demand instead of Run always
then I get the following message:
The script will be executed only if you add a specific request parameter: hidden link
Do I need to specify on demand - you say in question 2 above about Search and Pagination that I "should already be passing the URL values", if so where do i insert the specific request
parameter: "hidden link"?
Thanks
Hi Shane,
Trying to test this.
Please could you supply me some valid php code snippet (eg. Hello World) to help me check it's set up ok.
How does clicking the Search button find and run the right php code snippet?
I have attached the debugging mode section in wp-config.php - where can I see the relevant logs?
Thanks
Hi David,
You can actually leave the settings as they are. Given that this is a filter hook, it is constantly listening for our Views Queries. This means that whenever a view is loaded the filter hook is triggered.
This is why we specify the ID of the view in the code so the hook knows exactly which view to modify the query for.
So if your view ID doesn't match the ID that the hook is looking for then nothing will happen. In order to see what is happening to your view, you will need to enable the views debugging by going to Toolset -> Settings -> Frontend and enable the views debugger from this page.
What it does is that whenever a view is loaded a popup will be generated with the query information in that view. This should display the SQL query thats being used as well as a few other relevant information for you view.
Thanks,
Shane
Hi Shane, Appreciate your help.
Everything seems to be setup correctly but I'm not seeing any responses , no logs, no errors, no results.
Please could you send me a sample php code snippet and the View Loop Editor to display results so that I have a starting template to begin testing.
Thanks very much
David