Support » Plugin: Relevanssi - A Better Search » lib/search.php:135 – Notice: Undefined variable: term_id [ possible fix ]

  • FWI, I’m getting this error from Relevanssi 3.5.

    Notice:
    
    Undefined variable: term_id
    
    Location:
    
    wp-content/plugins/relevanssi/lib/search.php:135
    
    Call Stack:
    
    relevanssi_search()
    relevanssi_do_query()
    relevanssi_query()
    apply_filters_ref_array('the_posts')
    WP_Query->get_posts()
    WP_Query->query()
    WP->query_posts()
    WP->main()
    wp()
    
    Component:
    
    Plugin: relevanssi

    https://wordpress.org/plugins/relevanssi/

Viewing 15 replies - 1 through 15 (of 28 total)
  • Looks like you can patch this by just checking is term_id is set before operating on it. Since your goal is to throw it into a loop right after, try:

    if ( ! isset($term_id ) ){
    
    	// set to empty array before the next check
    	$term_id = array();
    }
    
    if (!is_array($term_id)) {
    	$term_id = array($term_id);
    }
    foreach ($term_id as $t_id) {
    	...
    }
    Plugin Author Mikko Saari

    (@msaari)

    Can you show me the tax_query you were trying to use when you got this error?

    I wasn’t. I just typed in a random string like ‘asdfasdf’ to see a search page. It was a fresh install so not everything was setup.

    Hello,

    Problem reproduced in same condition (same version, random search that has no result).
    Notice is on line 135 and 136.
    I use the search box with woocommerce.

    Anyway, thank you for this great plugin !

    Plugin Author Mikko Saari

    (@msaari)

    That’s tax_query processing code and only run when a tax_query is involved, so somewhere there’s a tax_query that’s malformed. Put this in your theme functions.php and let me know what it says:

    add_filter('relevanssi_modify_wp_query', 'rlv_test_tax');
    function rlv_test_tax($q) {
        var_dump($q->query_vars['tax_query']);
        var_dump($q->tax_query);
        return $q;
    }

    Thank you for your great support !

    Here is the dump

    array(1) {
    [0]=>
    array(4) {
    [“taxonomy”]=>
    string(8) “language”
    [“field”]=>
    string(16) “term_taxonomy_id”
    [“terms”]=>
    int(12)
    [“operator”]=>
    string(2) “IN”
    }
    }
    object(WP_Tax_Query)#10063 (6) {
    [“queries”]=>
    array(1) {
    [0]=>
    array(5) {
    [“taxonomy”]=>
    string(8) “language”
    [“terms”]=>
    array(1) {
    [0]=>
    int(12)
    }
    [“field”]=>
    string(16) “term_taxonomy_id”
    [“operator”]=>
    string(2) “IN”
    [“include_children”]=>
    bool(true)
    }
    }
    [“relation”]=>
    string(3) “AND”
    [“table_aliases”:protected]=>
    array(1) {
    [0]=>
    string(25) “wp1234_term_relationships”
    }
    [“queried_terms”]=>
    array(1) {
    [“language”]=>
    array(2) {
    [“terms”]=>
    array(1) {
    [0]=>
    int(12)
    }
    [“field”]=>
    string(16) “term_taxonomy_id”
    }
    }
    [“primary_table”]=>
    string(12) “wp1234_posts”
    [“primary_id_column”]=>
    string(2) “ID”
    }

    I am using ACF + Polylang, not sure if that helps.

    array(1) {
      [0]=>
      array(4) {
        ["taxonomy"]=>
        string(8) "language"
        ["field"]=>
        string(16) "term_taxonomy_id"
        ["terms"]=>
        int(81)
        ["operator"]=>
        string(2) "IN"
      }
    }
    object(WP_Tax_Query)#365 (6) {
      ["queries"]=>
      array(1) {
        [0]=>
        array(5) {
          ["taxonomy"]=>
          string(8) "language"
          ["terms"]=>
          array(1) {
            [0]=>
            int(81)
          }
          ["field"]=>
          string(16) "term_taxonomy_id"
          ["operator"]=>
          string(2) "IN"
          ["include_children"]=>
          bool(true)
        }
      }
      ["relation"]=>
      string(3) "AND"
      ["table_aliases":protected]=>
      array(1) {
        [0]=>
        string(21) "wpxyz_term_relationships"
      }
      ["queried_terms"]=>
      array(1) {
        ["language"]=>
        array(2) {
          ["terms"]=>
          array(1) {
            [0]=>
            int(81)
          }
          ["field"]=>
          string(16) "term_taxonomy_id"
        }
      }
      ["primary_table"]=>
      string(8) "wpxyz_posts"
      ["primary_id_column"]=>
      string(2) "ID"
    }

    I’m also using polylang. If I disable polylang the error go away ๐Ÿ™‚

    Yea, absolutely not an option for me on black-buddha.com. What’s the best course of action here?

    Plugin Author Mikko Saari

    (@msaari)

    Does the Polylang compatibility work for you? Do you get results in the correct languages?

    Looks like Relevanssi can’t handle the tax_query where “field” is set to “term_taxonomy_id”. That’s not a surprise, as that isn’t actually a legal value โ€“ย it should be “term_id”, not “term_taxonomy_id”. That’s why you’re seeing the error. Time to send some feedback to Polylang developers…

    Anyway, try adding this to your theme functions.php:

    add_filter('relevanssi_modify_wp_query', 'rlv_polylang_fix');
    function rlv_polylang_fix($q) {
        $q->query_vars['tax_query'] = null;
        return $q;
    }

    That should make the error go away. Does the language filtering still work?

    The languages work just fine in Polylang when you run the form through ‘get_search_form’.

    $form = apply_filters( 'get_search_form', $form );

    The ‘rlv_polylang_fix’ does not change anything. It does appear that the errors only show in the default language, in my case, English.

    Plugin Author Mikko Saari

    (@msaari)

    Ok, so the other languages probably result in a well-formed tax_query then.

    Does this work any better?

    add_filter('relevanssi_modify_wp_query', 'rlv_polylang_fix');
    function rlv_polylang_fix($q) {
        $q->tax_query = null;
        return $q;
    }

    Sorry, that did not work. Here are the rows passing into the function.

    English

    => Array
    (
        [taxonomy] => language
        [field] => term_taxonomy_id
        [terms] => 81
        [operator] => IN
    )

    Chinese

    => Array
    (
        [taxonomy] => language
        [terms] => Array
            (
                [0] => zh_tw
            )
    
        [field] => slug
        [operator] => IN
        [include_children] => 1
    )

    I tried fixing the terms using;

    // in functions.php
    add_filter('relevanssi_modify_wp_query', 'rlv_polylang_fix');
    
    function rlv_polylang_fix($q) {
        $tq = $q->query_vars['tax_query'];
        foreach($tq as $inx => $t){
           if( isset($t['terms']) && is_scalar($t['terms']) ){
    
               // fix to an array
               $q->query_vars['tax_query'][$inx]['terms'] = array($t['terms']);
    
               // try to set in term_id
               if(!isset($t['term_id'])){
                   $q->query_vars['tax_query'][$inx]['term_id'] = array($t['terms']);
               }
           }
        }
        return $q;
    }

    Nada.

    But after I set up some debugging, your logic doesn’t support term_taxonomy_id. If you add this, it’ll work:

    // in lib/search.php
    
    if ( $row['field'] == 'id' || $row['field'] == 'term_id' ) {
    	//..
    } elseif ( $row['field'] == 'term_taxonomy_id' && isset($row['terms'])) {
    	$id = $row['terms'];
    	if( is_array($id) ){
    		$term_id = $id;
    	} elseif(is_scalar($id)){
    		$term_id = array($id);
    	}
    }
    Plugin Author Mikko Saari

    (@msaari)

    Relevanssi isn’t supposed to support term_taxonomy_id, because according to WordPress documentation, that’s not an option. Legal values are term_id, name and slug.

    Also, Relevanssi already handles the Polylang support elsewhere, so this taxonomy parameter is not even needed.

    The best solution would be to clear out the taxonomy parameter so that Relevanssi doesn’t see it. Other way to handle it would be to modify the taxonomy query so that it’s passing a slug (like it’s doing for Chinese in your code) or a term_id.

Viewing 15 replies - 1 through 15 (of 28 total)
  • The topic ‘lib/search.php:135 – Notice: Undefined variable: term_id [ possible fix ]’ is closed to new replies.