Skip Navigation

[Resolved] specify AND condition for custom search filter (taxonomy) in wordpress archive

This support ticket is created 4 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

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)

This topic contains 12 replies, has 4 voices.

Last updated by davelow 4 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#1830311

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?

What is the link to your site?
hidden link

#1831033

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. **

#1832019

Hello Dave,

Views are different from archive templates. In a view, we can define the query of posts that will be used, we can also define query filters on taxonomies. But in archive templates, the query is provided by WordPress, and we can't define a query filter on the taxonomy.
Still, we can hook into the archive query and change the condition on the taxonomy query($tax_query['relation']= 'AND').
- https://developer.wordpress.org/reference/hooks/pre_get_posts/
- https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters

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.

#1833169

Yes Jamal, that is ok. I have a backup copy which can be deployed easily.

Dave

#1833389

I just want to let you know that I am still working on this. I have tried to hook into the archive query to no avail.

I'll check with the team for assistance and will get back to you as soon as possible.

#1833477

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.

#1833989

Hi Jamal,

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.

I hear from you soon.

thanks,
dave

#1836481

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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?

#1836751

Hi Minesh,

Thank you for taking this up.

"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.

thanks,
Dave

#1839105

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Dave

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":

add_action( 'pre_get_posts', 'ts_pre_get_posts', 101 );
function ts_pre_get_posts( $query ){

    if ( $query->is_post_type_archive( 'thing' ) && $query->is_main_query() ) {

        $tax_query = $query->get( 'tax_query' );

        if ( !empty( $tax_query ) ){

            $tax_query[0]['operator'] = 'AND';
            $query->set( 'tax_query', $tax_query );
        }
    }
}

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.

#1839287
Screenshot 2020-11-10 at 02.47.45.png
Screenshot 2020-11-10 at 02.25.06.png

Thank you Nigel,

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?

Thank you for your time.
Dave

#1839755

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

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.

#1839769

My issue is resolved now. Thank you!