Home › Toolset Professional Support › [Resolved] filter my taxonomy query before displaying my custom search
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 |
---|---|---|---|---|---|---|
- | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | - |
- | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | - |
Supporter timezone: Europe/London (GMT+00:00)
Tagged: Custom search, Views plugin
Related documentation:
This topic contains 16 replies, has 2 voices.
Last updated by martinE-4 4 years, 9 months ago.
Assisted by: Nigel.
I am trying to do a taxonomy search in a classic view with a subset of my taxonomy terms.
eg. my taxonomy is 'Resource Type" and it has hierarchical terms like in the attached pic
I want to just have a page which displays 'Seminal Publications' and the sub-terms of 'Reports', 'Journals' and 'Books'
Here is my filter control (which works for the multi-select but it shows all taxonomy terms):
[wpv-control-post-taxonomy taxonomy="resource-type" orderby="term_group" order='DESC' type="multi-select" url_param="wpvresourcetype" output="bootstrap"]
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Hi Martin
Views doesn't offer any means to customise the terms included in a taxonomy filter, all terms are included.
This has certainly come up before, and there is an existing internal request ticket for this feature.
I suggest you add your voice to that at https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/ so that the product manager is aware of the demand when determining development priorities.
As of now, the only option is to manipulate the options using JavaScript to remove the terms you do not want to appear.
Thanks. I put in a request.
What about this function:
https://toolset.com/forums/topic/filter-returned-values-for-wpv-control-post-taxonomy/
Would this help me pre-filter on the page with my taxonomy checkbox search?
Specifically:
function filter_parent_posts( $query ){ global $post; if ( $post->ID == 9999 && $query->query_vars['taxonomy'][0] == 'resource-type' && !is_admin() ) { $meta_query_args = array( 'relation' => 'AND', // Optional, defaults to "AND" array( 'key' => 'wpcf-resource-type', 'value' => 'Books','Reports','Journals', 'compare' => '=' ) ); $meta_query = new WP_Meta_Query( $meta_query_args ); $query->meta_query = $meta_query; } } add_action( 'pre_get_terms', 'filter_parent_posts',10,1);
Where:
- Replace 9999 with the page/post ID where you added the view
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
You would need to do more than just replace the page ID, you'd need to update the details of the query instead.
When Views populates the taxonomy filter options, it has to perform a query to get the terms to populate it with.
Views doesn't itself offer any API hooks to intervene in this process, although given that it uses standard WordPress functions to query for the terms, you can instead try the lower level WordPress APIs to intervene instead, which is what the above code is doing with the pre_get_terms filter. You can modify the query arguments, the challenge being to only intervene where required.
Ok, I'll try:
function filter_parent_posts( $query ){ global $post; if ( $post->ID == 2603 && $query->query_vars['taxonomy'][0] == 'resource-type' && !is_admin() ) { $meta_query_args = array( 'relation' => 'OR', // Optional, defaults to "AND" array( 'key' => 'wpcf-resource-type', 'value' => 'Seminal Publications', 'compare' => '=', ), array( 'key' => 'wpcf-resource-type', 'value' => 'Books', 'compare' => '=', ), array( 'key' => 'wpcf-resource-type', 'value' => 'Reports', 'compare' => '=', ), array( 'key' => 'wpcf-resource-type', 'value' => 'Journals', 'compare' => '=', ), ); $meta_query = new WP_Meta_Query( $meta_query_args ); $query->meta_query = $meta_query; } } add_action( 'pre_get_terms', 'filter_parent_posts',10,1);
Must be something wrong with this function because the complete number of posts display (not the subset), and the search part of the view doesn't display, just the results.
I fixed the extra comma after last compare but still no go:
array( 'key' => 'wpcf-resource-type', 'value' => 'Journals', 'compare' => '=', ) );
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
I'll need to take a closer look, but bear in mind this is unsupported custom code.
I didn't have time to look into it today, but I will tomorrow and I'll update you then.
Thanks that would be awesome! My client is asking me to reproduce in WordPress and Toolset the functionality of a Drupal site. Specifically these pages are currently stumping me:
hidden link
hidden link
You can see how the search filters at the top of the Drupal views allow the user to filter further for the given taxonomies.
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Sorry for the delay getting back to you, we had an unexpected spike in forum threads, I haven't forgotten you and should be able to review this in the morning.
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Hi Martin
I tried to identify the simplest way to specify which terms would be included in the taxonomy filter, and that is to specify the IDs of the terms to be included in an array.
I think your use-case is simple enough that you can look up the IDs of the terms you want to include.
You would then need the following code:
function ts_mod_filter_terms( $query ){ $tax = 'resource-type'; // Edit taxonomy slug $termids = array( 6, 10, 15 ); // Edit IDs of terms to include in filter if ( is_page( 'view-filtered-things' ) && $query->query_vars['taxonomy'][0] == $tax ) { $query->query_vars['include'] = $termids; } } add_action( 'pre_get_terms', 'ts_mod_filter_terms' );
Can you try with that, editing the IDs as required.
Still problems:
my function:
function ts_mod_filter_terms( $query ){ $tax = 'resource-type'; // Edit taxonomy slug $termids = array( 4, 21, 22, 23 ); // Edit IDs of terms to include in filter if ( is_page( 'seminal-publications' ) && $query->query_vars['taxonomy'][0] == $tax ) { echo "Im here"; $query->query_vars['include'] = $termids; } } add_action( 'pre_get_terms', 'ts_mod_filter_terms' );
Proof of its function:
Im hereIm hereIm hereIm here (echoes 4 times to screen just before the view starts)
Filter added automatically when I insert the Resource Type taxonomy filter in the view (code below):
Resource types are: Set by one URL parameter
The resource-type slug is any of the values coming from the
URL parameter wpv-resource-type
Taxonomy relationship:
Relationship to use when querying with multiple taxonomies AND
Filter code in view:
[wpv-filter-start hide="false"] <div class="form-group"> <label for="wpv-resource-type">[wpml-string context="wpv-views"]Resource types[/wpml-string]</label> [wpv-control-post-taxonomy taxonomy="resource-type" type="select" default_label="Select Type" url_param="wpv-resource-type"] </div>[wpv-filter-controls] [wpv-filter-end]
Custom Search Settings:
- let me choose individual settings manually
- Update the View results every time an input changes
- Update URLs after loading search results (also tried 'Always show all values for inputs' but no change)
- Show only available options for each input (with Hide option on Select dropdown)
Result:
A drop down filter for Resource types showing only the default label (no terms)
225 results found (this is all the resources not a subset as hoped)
Summary:
The page with the view shows all my Resource posts (225) (regardless of the resource-type filter). The drop-down select shows no terms. So the function although appearing to fire 4 times is not doing what we expect?
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
I re-tested the same code on my site and it works as expected when I create a View without the auto-updating results, i.e. with a vanilla search View that reloads the page after visitors hit the submit button.
Could you check the same on your site to know if the code is working as expected at all?
If so, I can look into what else may be required when using the ajax settings.
Sorry, I made a mistake of having two function versions activated. Partial success (see below)
using the function you recommend:
function ts_mod_filter_terms( $query ){ $tax = 'resource-type'; // Edit taxonomy slug $termids = array( 4, 21, 22, 23 ); // Edit IDs of terms to include in filter if ( is_page( 'seminal-publications' ) && $query->query_vars['taxonomy'][0] == $tax ) { echo "Im here"; $query->query_vars['include'] = $termids; } } add_action( 'pre_get_terms', 'ts_mod_filter_terms' );
(with the added echo line to see It fires)
Result:
-it appears to fire 4 times when the page is opened, presumably once for each taxonomy slug id in the filter (" Im hereIm hereIm hereIm here" is displayed at beginning of content)
Using either of the Custom search Settings which do NOT use AJAX:
- initially a full 225 results are displayed and correctly I get only the 4 desired taxonomy items in the select. When I search any of the 4 taxonomy items they give me 24 results which is the correct size of the subset for all 4 taxonomy IDs but the different sub-selections do not seem to work (ie. they all return the 24 subset items instead of the correct number in each of my taxonomy id values).
Using either of the 3 last Custom search Settings which DO use AJAX:
- initially a full 225 results are displayed and correctly I get only the 4 desired taxonomy items in the select. When I search any of the 4 taxonomy items they give the correct sub-groups but the select dropdown changes to ALL the values possible (not just the 4 IDs I want to focus on) - so essentially I am not gaining much with the pre-filter attempts here.
Sorry, I guess we should give up here since I got a message from one of the devs (Juan) that they are hoping to implement this soon:
I am writing to you regarding the feature request you filled, related to frontend search filters by taxonomies and the lack of ability to select just a number of terms to be offered, instead of all the terms in the taxonomy.
I am pleased to let you know that we are getting back to this task again, and it will be implemented in the near future.