This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Search with categories 'OR' vs. 'AND'

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Search with categories 'OR' vs. 'AND'

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #4715
    dfittdfitt
    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 MarcinkoErnest 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:

    [code]
    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’)";
    }
    [/code]

    3. Modify those lines to this:

    [code]
    if (count($all_terms) > 0) {
    $term_query = "HAVING (ttid REGEXP ‘–" . implode("–‘ AND ttid REGEXP ‘–", $all_terms) . "–‘)";
    }
    [/code]

    Now, if you have all the other categories excluded but those two on the Advanced options panel, then this should probably work.

    #4717
    dfittdfitt
    Participant

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

    #4718
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Should work with any number, at least it does on my test environment.

    #4719
    dfittdfitt
    Participant

    Thanks again.

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.