Tell us what you are trying to do?
I am trying to make the custom search filter in the archive to use AND logic instead of OR logic
Is there any documentation that you are following?
Is there a similar example that we can see? hidden link is constructed by a view, and i could specify in the content selection of the view to use a query filter with AND logic applied on taxonomy.
for specific custom types, i chose to use an archive. The aim is to achieve the same as the main directory. In these archives, I use a custom search filter (see the link provided in the next question). How can I specify to use 'AND' logic instead of 'OR' logic?
Hello and thank you for contacting the Toolset support.
Search is by default performed with an AND operator between the different filters, for example, if you put two taxonomy filters and you search for posts that have "termA" for "taxonomyA" and "termB" for "taxonomyB", Toolset will return the posts that have termA and termB.
The OR operator is used within the same filter. For example, if you filter by termA and "termC" in "taxonomyA", Toolset will return posts that have either "termA" OR "termC". Similar for checkboxes fields.
Can you provide more details about your use case? What filters you are using? What would you expect for certain filter values?
It would also help if you allow me temporary access to your site to check it closely. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **
If you need assistance with this part, I'll need to disable AJAX on the archive while debugging, let me know if that's ok, or provide a staging site where I can test.
Hello Dave, I am really sorry to tell you, that I mistakenly deleted an existing custom code snippet in Toolset->Settings->Custom code, while I was working on a custom snippet to enforce the "AND" operator. Please restore your backup to recover it.
Thank you for the email.
I think what those codes intend to do have been achieved by inherent plugin designs. They are not active so it does not affect anything. nevertheless i have recovered the site with the backup, so no worries.
Jamal is on Vacation. This is Minesh here and I'll take care of this ticket. Hope this is OK.
I see with the taxonomy Services, the term "Creative Grooming" is child term of "Dog Grooming".
=> hidden link
As I understand, you still want that if I check mark both "Creative Grooming" and "Dog Grooming", you want that only post where both terms are checked should be displayed - correct?
Also, I see that you assigned the same archive to multiple post types:
=> hidden link
You mean, you want to apply "And" clause to only "groomers" post type archive?
"As I understand, you still want that if I check mark both "Creative Grooming" and "Dog Grooming", you want that only post where both terms are checked should be displayed - correct?"
Yes, the AND logic should also apply for parent and child terms.
"You mean, you want to apply "And" clause to only "groomers" post type archive?"
No, the AND logic should apply to all the assigned post types.
my guess is it will work out ok because when viewing an archive, wordpress will by default have provided the first level filter in that only the post type I am viewing will show up in the archive? for example, if i am viewing the 'groomers' archive, only 'groomer' posts will show, and the custom search will provide a second level filter with the AND logic.
Sorry to bump you around, Jamal is still on vacation and Minesh is now sick, let me help out.
It's a little tricky changing the condition for the taxonomy query on the archive page, you need to use the WordPress pre_get_posts action and be sure you are targeting the right query.
Am I right in thinking you only have one taxonomy filter on this archive?
If so, you can add a code snippet like the following to change the comparison from "IN" to "AND":
You'll need to change the slug of the post type (thing in my code), and you can include an array of post type slugs if this is to apply to more than one post type archive.
If you have more than one taxonomy filter then it becomes a little more complicated, but try that and let me know how you get on.
It looks like its working. however, on a closer look there seems to be an issue for parent terms.
For terms 'dog-grooming' and 'dog-training' which have child terms, it does not work as expected.
Or was it something I have missed out?
Yes, there will be more taxonomy filters to add.
I've noticed that "Show only filter options that would produce results" is dynamic in a view, but is not for an archive.
if you compare the archive (hidden link) with the page created with a view ( hidden link), we can see that the filter options updates in the view. Is that a limitation for archive?
I took another look and saw that the original code was tripping up if there were child terms involved, which is fixed by updating the include_children option to false.
If you are going to add other taxonomy filters then I also updated the code to make it more generic: you'll need to provide the post types of the archives in question and the slugs of the taxonomies which have filters to change.
add_action( 'pre_get_posts', 'ts_pre_get_posts', 101 );
function ts_pre_get_posts( $query ){
$post_type_archives = array( 'thing' ); // Post types archives to modify
$taxonomies = array( 'colour' ); // Taxonomy filters to change to AND comparison
if ( $query->is_post_type_archive( $post_type_archives ) && $query->is_main_query() ) {
$tax_queries = $query->get( 'tax_query' );
if ( !empty( $tax_queries ) && is_array( $tax_queries ) )
{
foreach ($tax_queries as $key => $tax_query)
{
if ( is_array( $tax_query ) && in_array( $tax_query['taxonomy'], $taxonomies ) )
{
$tax_queries[$key]['operator'] = 'AND';
$tax_queries[$key]['include_children'] = false;
}
}
$query->set( 'tax_query', $tax_queries );
}
}
}
Regarding the setting for "Show only filter options that would produce results" it is available with archives, and it works (I tested that locally to be sure). However, as we are adding custom code to modify the query results it is to be expected that it may no longer work as expected, and so I suggest you disable that option on any archive that you are modifying.