Home › Toolset Professional Support › [Resolved] Filter custom post with a taxonomy filter
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)
Tagged: Views, Views plugin
This topic contains 10 replies, has 2 voices.
Last updated by Timothy 4 years, 4 months ago.
Assisted by: Minesh.
I have a cpt called Faculty Members with 2 taxonomies "Leadership Category" and "Faculty Category". I created a View that displays the Faculty, but I want to only display the Faculty members that are in a Faculty Category not a Leadership Category, and have the front-end filters only show the Categories from the Faculty Category. I currently have the View embedded here:
hidden link
Everything works except on initial page load it displays all faculty members even if they are in a Leadership Category and not in any Faculty Category. So it appears the All initial filter does not actually filter according to the Taxonomy filter "Faculty Category" that I have the filter set to.
[wpv-control-post-taxonomy taxonomy="faculty-category" type="radios" default_label="All " url_param="wpv-faculty-category"]
Tim
Hello. Thank you for contacting the Toolset support.
Yes - By default, view displays all results.
Do you mean that when view load the first time - you only want to displays the posts which belongs to category "Faculty Category".
If this is true- we need to add a query filter to pre-filter the view results.
If you can share access details, I am happy to share the filter.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) 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.
Yes, when view load the first time I only want to displays the posts which belongs to category "Faculty Category". Can you just let me know where/how to add that query filter and I can do it myself.
Thanks,
Tim
You can use the view's filter hook - wpv_filter_query
Where:
- you should try to get all the posts IDs attached to taxonomy "Faculty Category" and use the post__in query argument and assign the found IDs.
For example:
- Add the following code to "Custom Code" section offered by Toolset
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/
or
- to the functions.php file of your theme.
add_filter( 'wpv_filter_query', 'func_remove_unwanted_category_posts', 99, 3 ); function func_remove_unwanted_category_posts( $query_args, $view_settings, $views_id ) { if ( $views_id == 9999 and !isset($_GET['wpv-faculty-category']) ) { /// YOUR CODE GOES HERE $found_post_ids = get the post IDs belongs to "Faculty Category"; $query_args['post__in'] = $found_post_ids; } return $query_args; }
Where:
- Replace 9999 with your original view ID and adjust the code accordingly
More info:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
If you do not know how to do it, please let me know.
I cannot get this to work, I also tried the solution here but that too does not seem to work:
https://toolset.com/forums/topic/display-all-posts-from-1-taxonomy/#post-1227954
Tim
Can you please share access details so I can login to your system and check whats going wrong or adjust the code accordingly.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) 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.
Can you please check now: hidden link
I've added the following code to "Custom Code" section offered by Toolsest:
=> hidden link
add_filter( 'wpv_filter_query', 'func_prefilter_post_taxonomy', 99, 3 ); function func_prefilter_post_taxonomy( $query_args, $view_settings, $view_id ) { $display_view_ids = array(815); if (in_array($view_id,$display_view_ids) and empty($query_args['tax_query']) ){ $query_args['tax_query'] = array(array( 'taxonomy'=> 'leadership-category', 'field' => 'id', 'terms' => array(49,50,51), 'operator' => 'NOT IN', 'include_children' => 1 )); } return $query_args; }
More info:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/
Can you please confirm it works as expected now.
Can you please confirm the solution I shared help you to resolve your issue.
Hi Minesh,
This sort of works. It does exclude people who are in the Leadership taxonomy from appearing in "All". But if a Faculty Member is in a Faculty taxonomy AND a Leadership taxonomy then they too are excluded from "All". So for example if you click Music History you will see "Neal" as he is in Faculty > Music History. But because he is also in the Leadership taxonomy he does not appear in 'All" and he should.
Is there a way around this?
Tim
Can you please check now: hidden link
i've adjusted the "wpv_filter_query" hook code as given under at "Custom Code" section of Toolset:
=> hidden link
add_filter( 'wpv_filter_query', 'func_prefilter_post_taxonomy', 99, 3 ); function func_prefilter_post_taxonomy( $query_args, $view_settings, $view_id ) { $display_view_ids = array(815); if (in_array($view_id,$display_view_ids) and empty($query_args['tax_query']) ){ $query_args['tax_query'] = array( 'relation' => 'OR', array( 'taxonomy'=> 'faculty-category', 'operator' => 'EXISTS', ), array( 'taxonomy'=> 'leadership-category', 'field' => 'id', 'terms' => array(49,50,51), 'operator' => 'NOT IN', 'include_children' => 1 ) ); } return $query_args; }
Can you please confirm now it works as expected.
My issue is resolved now. Thank you!