Skip Navigation

[Resolved] OR Logic for some taxonomies in wordpress archive

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 10 replies, has 2 voices.

Last updated by fahimS-2 1 year, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#2574241

In a wordpress archive I am implementing a search filter for the users. I have 5 custom taxonomies which I have added as search filter in the archive. But for only 3 of them, I want to have OR logic between each other. How can I do that?

#2574893

Hello,

In your case, it needs custom codes, for example, use WordPress Built-in action hook "pre_get_posts" to trigger a custom PHP function:
https://developer.wordpress.org/reference/hooks/pre_get_posts/

Check if it is specific WordPress archive query, then change the AND/OR logic of taxonomy filters as what you want:
https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters

#2574995

Hi, thanks for your reply. Would you mind giving me the code?

#2576601

Here is a sandbox website:
hidden link
You can produce the same problem in above website, also point out the WordPress Archive page URL, and taxonomy filters you want to setup, I can setup a demo for you.

#2579039

Hi there,
I was trying to create the same problem in your given site. I created an archive and assigned it to the home page. The archive url is: hidden link

But the archive isn't appearing in the home page. Default archive is appearing. Can you kindly check it out.

Thank you.

#2579067

It just needs to switch to 2021 theme, please provide details for this:
What kind of AND/OR logic do you want to setup for those taxonomy filters, I need to test and debug it in above test website.

#2579167

Hi there,
Thanks for the solution. I can't login with the link you provided any more. Can you kindly check that out?

#2579627

You can use below URL to login into the sandbox website:
hidden link

please provide details for this:
What kind of AND/OR logic do you want to setup for those taxonomy filters, I need to test and debug the custom codes in above test website.

#2580625

I have replicated the problem in an archive. The link to the archive is: hidden link

All the taxonomy queries have 'AND' logic between each other by default. I want to have OR logic between Book Year, Book Author and Book Publication.

I also have added this custom code for pre_get_posts hook, which adds custom posts to the home archive. I am providing the code so that you can have a look, how the new code for OR relationship will integrate with this custom post code.

function post_types_author_archives($query) {
    if ($query->is_author() || ($query->is_home() && $query->is_main_query()))
            // Add 'books' CPT and the default 'posts' to display in author's archive
            $query->set( 'post_type', array('saas', 'saas-1', 'post') );
    remove_action( 'pre_get_posts', 'custom_post_author_archive' );
}
add_action('pre_get_posts', 'post_types_author_archives');

Thanks for your patience. Looking Forward To Your Reply.

#2581063

I have done below modifications in above sandbox website:
Edit the custom code snippet "add-cpt":
hidden link
add lines 17~30:

function taxonomies_filter_archives($query) {
  	$tax_slugs = array('book-year', 'book-author', 'book-publication'); //taxonomy slugs
    if ( $query->is_home() && $query->is_main_query() && isset($query->query_vars['tax_query']) ){
      	$book_tax_query['relation'] = 'OR';
    	foreach($query->query_vars['tax_query'] as $k => $v){
          	if(!in_array($v['taxonomy'], $tax_slugs)) continue;
        	$book_tax_query[] = $v;
        	unset($query->query_vars['tax_query'][$k]);
        }
      if(count($book_tax_query) > 1){
      	$query->query_vars['tax_query'][] = $book_tax_query;
      }
    }
    remove_action( 'pre_get_posts', 'taxonomies_filter_archives', 99 );
}
add_action('pre_get_posts', 'taxonomies_filter_archives', 99);

Test it in frontend:
hidden link

It works fine, for your reference.

#2584021

My issue is resolved now. Thank you!