• Hi;

    When i search by multiple categories AND search keyword, the result set is empty although all search criteria are met. The same search query returns the expected results when i use the wordpress default search instead of relevanssi.

    Is that a known limitation or can it be fixed ?

    example:

    All of the following queries should return at least one entry:

    (is ok) https://blog.machinimatrix.org/?cat=62&s=random
    (is ok) https://blog.machinimatrix.org/?cat=62&type=541&s=
    (broken) https://blog.machinimatrix.org/?cat=62&type=541&s=random

    However the last query returns nothing.

    ====

    details: I have setup a search form which creates 3 parameters:

    cat (main search category)
    type (subcategory)
    s (search keyword)

    I have added an action for ‘parse_request’ into functions.php which changes the $query->query_vars. When i now call the blog as follows:

    https://the.blog.org/?s=mysearch&cat=1&type=2

    Then the parse_request function changes the $query->query_vars to:

    Array ( [s] => mysearch [category__and] => Array ( [0] => 1 [1] => 2 ) )

    Maybe something is missing here ?

    ====

    The function code:

    add_action( 'parse_request', 'category_search_logic', 11 );
    function category_search_logic( $query ) {
    
        // Nothing that we should care about:
        if ( ! isset( $query->query_vars[ 'cat' ] ) )
        {
            return $query;
        }
    
        // Store session variables for later use
        if(isset($_GET['cat'])) {
            // split categories query on a space to get IDs separated by '+' in URL
            $categories = explode( ' ', $_GET[ 'cat' ] );
            $_SESSION['cat'] = $categories;
        }
        else $categories = '';
    
        if(isset($_GET['type'])) {
            $subcategory = $_GET['type'];
            $_SESSION['type'] = $subcategory;
        }
        else {
            $subcategory= '';
            $_SESSION['type'] = 0;
        }
    
        if ( $categories == '' ) return $query;
    
        // create list of restrict categories
        if ($subcategory != '' && $subcategory != 0) {
           array_push($categories, $subcategory);
        }
    
        if ( count( $categories ) > 1) {
            unset( $query->query_vars[ 'cat' ] );
            $query->query_vars[ 'category__and' ] = $categories;
        }
    
        return $query;

    https://www.ads-software.com/extend/plugins/relevanssi/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Gaia Clary

    (@gaia-clary)

    The problem seems to be that the relationship between wp_term_taxonomy and wp_terms is not one to one. Actually in my case the query:

    select * from wp_term_taxonomy where term_id in (62,541);

    returned 3 items (2 from the “categories” taxonomy and one from the “menu_nav” taxonomy)

    I could solve this problem by modifying the search.php (near line98) as follows:

    if ($row['field'] == 'id') {
        $id = $row['terms'];
        $term_id = $id;
        if (is_array($id)) {
            $id = implode(',', $id);
        }
        $term_tax_id = $wpdb->get_col(
           "SELECT term_taxonomy_id
            FROM $wpdb->term_taxonomy
            WHERE term_id IN ($id) and taxonomy='category'");
    }

    I am not sure if that breaks something else or restricts the search otherwise. But maybe that can help to sort out a more general solution ?

    Plugin Author Mikko Saari

    (@msaari)

    Ah, good catch. I first thought term_id is unique in wp_term_taxonomy, but it’s actually unique within the taxonomy.

    Anyway, your fix does break something – the taxonomy query is not only for categories, but for all taxonomies. However, having “taxonomy != ‘menu_nav'” should be safe…

    The search by slug actually restricts the search to correct taxonomy. It probably should be done like that in the ID search as well. Does this work?

    $term_tax_id = $wpdb->get_col(
    	"SELECT tt.term_taxonomy_id
    	FROM $wpdb->terms AS t, $wpdb->term_taxonomy AS tt
    	WHERE tt.term_id = t.term_id AND tt.taxonomy = '" . $row['taxonomy'] . "' AND t.term_id IN ($id)");
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘search by multiple categories AND keyword returns no results’ is closed to new replies.