Search with categories 'OR' vs. 'AND'

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 dfitt 8 years, 11 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #4715
    dfitt
    dfitt
    Participant

    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.

    #4716
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi!

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


    #4717
    dfitt
    dfitt
    Participant

    Thanks Ernest. Will this modification work for only two categories, or will it work for any number of included categories?

    #4718
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Should 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 :)


    #4719
    dfitt
    dfitt
    Participant

    Thanks again.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.