Home › Toolset Professional Support › [Resolved] Filter a list of posts based on taxonomy and post type
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 12 replies, has 3 voices.
Last updated by Minesh 1 year, 6 months ago.
Assisted by: Minesh.
Hi there,
This is a general question about using Toolset. In the past I've created filters that let me use taxonomies to winnow a list of posts down to those matching certain taxonomy keywords. But I limited it to a single post type. Is it possible to create a filter that uses one or more taxonomies *and* multiple custom post types?
For example, let's say I have Albums as a custom post type and Musical Artists as a custom post type. I also have a taxonomy named Adjectives. Can I create a page with a filter like the fake one below that will list posts matching the supplied criteria?
Type
[ ] Albums
[ ] Musical Artists
Adjectives
[ ] Cool
[ ] Mysterious
[ ] Chill
Thank you for your help!
Hi there,
I will answer the question before the ticket is assigned to one of my colleagues.
The first set of lists and the second one should be added separately as two Views method.
For each you will need to create a taxonomy view that lists the taxonomy in question and use another view nested to the taxonomy view's content template to list the custom posts.
You can use this as an example:
https://toolset.com/forums/topic/nested-views-not-filtering-by-parent-taxonomy/
Thanks.
Hi Christopher,
Thanks for replying to the ticket.
I understand that I need to create two views. I *think* one of them is a taxonomy view (for "Adjectives") and one is a post view (for "Type"). The post view for "Type" is nested in the taxonomy for "Adjectives." Is that correct?
How does one add the fields to a search filter? I'm attaching a sketch to illustrate what we want to accomplish.
Thank you!
Saul
As I understand - you have post type X and taxonomy A and B and another post type Y and taxonomy C and D.
You want to create a view where you want that you want to filter posts of both post types (X and Y) and you want to add taxonomy filter for taxonomy A and B that belongs to X post type as well as taxonomy C and D that belongs to Y post type. Is this you want or you want something else?
Minesh,
Thank you for your response. Your description of what I'm trying to accomplish is close: I have post types W, X, and Y, who all share taxonomies A, B, and C (which are all hierarchical taxonomies / categories with known vocabulary words). I want to create a view where I can filter the list of posts based on the post type and any of the taxonomy vocabulary words.
Let me try to clarify my previous ASCII sketch:
Media (post type)
[ ] Book (custom post type)
[ ] Music Album (custom post type)
[ ] Movie (custom post type)
Keyword (category taxonomy)
[ ] fun (category taxonomy vocabulary word)
[ ] surprising (category taxonomy vocabulary word)
[ ] uplifting (category taxonomy vocabulary word)
$Second-Taxonomy-Category (category taxonomy)
[ ] taxonomy vocabulary word 1
[ ] taxonomy vocabulary word 2
[ ] taxonomy vocabulary word 3
[ ] ...
$Third-Taxonomy-Category (category taxonomy)
[ ] taxonomy vocabulary word 1
[ ] taxonomy vocabulary word 2
[ ] taxonomy vocabulary word 3
[ ] ...
If I checked "Book" and "Music" under the "Media" post type, and checked "fun" under the "Keyword" taxonomy, I would expect to see a list of Book and Music custom posts associated with the Keyword "fun."
Does this make sense?
Saul
If I checked "Book" and "Music" under the "Media" post type, and checked "fun" under the "Keyword" taxonomy, I would expect to see a list of Book and Music custom posts associated with the Keyword "fun."
=====>
If I checked "Book" and "Music" under the "Media" post type,
-- so Media post type will hold "Book" and "Music" as taxonomy? what is the content structure here I do not understand.
I would expect to see a list of Book and Music custom posts associated with the Keyword "fun."
====>
Where you want to display this?
Each post type is different entity.
Can you please share problem URL where you want to display what using what post types and what fields/taxonomies and whats the relation between them and also share admin access details. You can share few screenshots as well that should help me to understand your content structure and share all required details about your requirement and end results.
Once I review that I will be able to guide you in the right direciton.
*** 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.
I've created the following view in legacy mode as there is some issues with block view on your site while I try to create the view.
So - following view is created in legacy mode:
=> hidden link
Where I've added post type filter and both taxonomy filter.
Then I've added the following code to "Custom Code" section offered by Toolset:
=> hidden link
add_filter('wpv_filter_register_url_parameters_for_posts','func_register_post_type_custom_filter_argument',99, 2);
function func_register_post_type_custom_filter_argument($attributes, $view_settings){
if($view_settings['view_id'] == 20){
$attributes[] = array(
'query_type'=> 'posts',
'filter_type'=> 'post_type',
'value'=> 'custom_field_value',
'attribute'=> 'wpv-post-type',
'expected'=> 'string',
);
}
return $attributes;
}
add_filter( 'wpv_filter_query', 'func_filter_by_post_type', 10, 3 );
function func_filter_by_post_type( $query_args, $settings, $view_id ) {
if ( $view_id == 20 && isset($_GET['wpv-post-type']) && !empty($_GET['wpv-post-type'][0]) ) {
$query_args['post_type'] = $_GET['wpv-post-type'][0];
}
return $query_args;
}
I've created the following test page and added the view using "Fields and Text" block:
=> hidden link
Can you please confirm it works as expected now:
- hidden link
Minesh,
This looks great! It's almost what my sketch looks like. The only difference is that instead of using pull-down menus to select a single post type or single taxonomy value, the sketch uses sets of checkboxes in order for users to select more than one post type or taxonomy value. Please refer to the attached image. How would I accomplish that?
Saul
Toolset can use bootsrrap:
- https://toolset.com/2019/07/toolset-now-supports-bootstrap-4/
So I've added the filters within the view's "Search and Pagination" section as given under:
=>hidden link
[wpv-filter-start hide="false"]
[wpv-filter-controls]
<div class="row">
<div class="col-sm-4">
<div class="form-group posttype">
<label>
[wpml-string context="wpv-views"]Post Type:[/wpml-string]</label>
[wpv-control name="post_type" type="checkboxes" values="case-study,story,news" display_values="Case Studies,Stories,News Articles" url_param="wpv-post-type" class="checkbox"]
</div>
</div>
<div class="col-sm-4"><div class="form-group">
<label for="wpv-audience">[wpml-string context="wpv-views"]Audiences[/wpml-string]</label>
[wpv-control-post-taxonomy taxonomy="audience" type="checkboxes" url_param="wpv-audience"]
</div></div>
<div class="col-sm-4"><div class="form-group">
<label for="wpv-cpt-year">[wpml-string context="wpv-views"]Years[/wpml-string]</label>
[wpv-control-post-taxonomy taxonomy="cpt-year" type="checkboxes" default_label="Select Year" url_param="wpv-cpt-year"]
</div></div>
</div>
[/wpv-filter-controls]
[wpv-filter-submit output="bootstrap" class="btn-secondary"]
[wpv-filter-end]
I've also added the following custom CSS to "Search and Pagination" section CSS box:
.posttype input[type="checkbox"]{
float:left;
}
You can format the loop output as required and you can follow the following Docs:
- https://toolset.com/documentation/legacy-features/views-plugin/
- https://toolset.com/documentation/legacy-features/views-plugin/view-layouts-101/
- https://toolset.com/documentation/legacy-features/views-plugin/front-page-filters/
Minesh,
This is fantastic. Thank you so much for going the extra mile. I deeply appreciate it!
Saul
Hi Minesh,
While testing the page a bit more, I discovered an issue with the filter: if I check multiple post types (e.g., both Stories and News Articles), only one post / post type appears. I think I just need to adjust your code like so:
function func_filter_by_post_type( $query_args, $settings, $view_id ) {
if ( $view_id == 20 && isset($_GET['wpv-post-type']) && !empty($_GET['wpv-post-type'][0]) ) {
// old line is below
// $query_args['post_type'] = $_GET['wpv-post-type'][0];
// revised line is below
$query_args['post_type'] = $_GET['wpv-post-type'];
}
return $query_args;
}
Is that correct?
Saul
When user mark the ticket as resolved the system automatically deletes the private information like access details.
Can you please send me admin access details again.
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 adjusted the code added to "Custom Code" section as given under:
add_filter( 'wpv_filter_query', 'func_filter_by_post_type', 10, 3 );
function func_filter_by_post_type( $query_args, $settings, $view_id ) {
if ( $view_id == 20 && isset($_GET['wpv-post-type']) && !empty($_GET['wpv-post-type']) ) {
$query_args['post_type'] = $_GET['wpv-post-type'];
}
return $query_args;
}
This worked like a charm. Thanks again, Minesh!