Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Search with categories 'OR' vs. 'AND'
This topic contains 4 replies, has 2 voices, and was last updated by dfitt 8 years, 4 months ago.
- AuthorPosts
- May 13, 2015 at 6:57 pm #4715
Hello:
I want to create a search box that uses two categories to filter the results with. When I include the two categories, I get posts categorized with either category A or category B, but I want to only get posts that are categorized with both category A and category B. Is there any way to change the OR to an AND? Thanks.
May 13, 2015 at 7:59 pm #4716Hi!
The logic is changeable, but it’s from an exclusion viewpoint for various reasons. (mostly because of how wordpress stores terms) By default it’s the AND logic that’s used, but it’s connected only with the exclusions.
I’m working on an update which will have a wider selection of term logics with a documentation as well.Until then the best possible thing to do is to make a minor modification to the code to force an AND logic on the non-excluded categories.
1. Open up the plugins/ajax-search-pro/includes/search_content.class.php file
2. Go to lines 182-188, where you should see this:if (count($all_terms) > 0) { $words = '--' . implode('--|--', $all_terms) . '--'; if ($term_logic == 'and') $term_query = "HAVING (ttid NOT REGEXP '$words')"; else $term_query = "HAVING (ttid REGEXP '$words')"; }
3. Modify those lines to this:
if (count($all_terms) > 0) { $term_query = "HAVING (ttid REGEXP '--" . implode("--' AND ttid REGEXP '--", $all_terms) . "--')"; }
Now, if you have all the other categories excluded but those two on the Advanced options panel, then this should probably work.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
May 13, 2015 at 8:32 pm #4717Thanks Ernest. Will this modification work for only two categories, or will it work for any number of included categories?
May 13, 2015 at 8:33 pm #4718Should work with any number, at least it does on my test environment.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
May 13, 2015 at 8:45 pm #4719Thanks again.
- AuthorPosts
You must be logged in to reply to this topic.