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?
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
Hi, thanks for your reply. Would you mind giving me the code?
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.
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.
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.
Hi there,
Thanks for the solution. I can't login with the link you provided any more. Can you kindly check that out?
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.
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.
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.
My issue is resolved now. Thank you!