wp_query not filtering tax_query correctly in pre_get_posts

蓝咒 提交于 2019-12-11 14:19:05

问题


I'm having a problem with the following situation: I am creating a custom form for searching my custom post type (immobiliare) and attached to this I have 2 custom taxonomies: location and tipologia.

My searchform.php is:

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
    <input type="hidden" value="proceed" name="s" id="s" />
    <fieldset>
      <legend>Ricerca per:</legend>
      <label class="screen-reader-text" for="query">Testo:</label>
          <input type="text" value="<?php the_search_query(); ?>" name="query" id="query" />
    </fieldset>
    <fieldset>
      <label class="screen-reader-text" for="s">Tipologia:</label>
          <?php wp_dropdown_categories(array(
            'hide_empty' => 0,
            'taxonomy' => 'tipologia',
            'name' => 'tipologia',
            'show_option_all' => 'Nessuna Preferenza'
          )); ?> 
    </fieldset>
    <fieldset>
      <label class="screen-reader-text" for="s">Località:</label>
          <?php wp_dropdown_categories(array(
            'hide_empty' => 0,
            'taxonomy' => 'location',
            'name' => 'location',
            'show_option_all' => 'Nessuna Preferenza'
          )); ?> 
    </fieldset>
    <input type="submit" id="searchsubmit" value="Search" />
</form>

As you can see, I have 3 fields: 1 is a textfield and the other 2 are dropdowns with my taxonomies. I want the user to be able to search for these fields. I had to hack the "s" input type because it would not let me submit empty queries and added a "query" field instead. My functions.php file is as follows:

<?php

  // Start LOGGING
  if(!function_exists('_log')){
    function _log( $message ) {
      if( WP_DEBUG === true ){
        if( is_array( $message ) || is_object( $message ) ){
          error_log( print_r( $message, true ) );
        } else {
          error_log( $message );
        }
      }
    }
  }
  // end LOGGING

  // start SEARCH FORM
  function ij_get_int($var){
    if (isset($var)) {
      $int_var = intval($var);
      if ($int_var > 0)
        return $int_var;
    }
    return false;
  }

  function ij_get_str($var) {
    $str_var = esc_attr($var);
    if (strlen($str_var) > 0)
      return $str_var;
    return false;
  }

  function custom_search_results($query) {
    if ($query->is_search && $query->is_main_query()) {
      $tax_query = array(
        'relation' => 'AND',
      );
      $location = ij_get_int($_GET['location']);
      $tipologia = ij_get_int($_GET['tipologia']);
      if ($location) {
        array_push($tax_query, array(
           'taxonomy' =>   'location',
           'field'    =>   'id',
           'terms'    =>   $location,
           'operator' =>   'IN'
        ));
      }
      if ($tipologia) {
        array_push($tax_query, array(
           'taxonomy' =>   'tipologia',
           'field'    =>   'id',
           'terms'    =>   $tipologia,
           'operator' =>   'IN'
        ));
      }
      $query->set('tax_query', $tax_query);

      $query->set('s', ij_get_str($_GET['query']));
      $query->set('post_type', 'immobiliare');
    }
  }

  add_action('pre_get_posts', 'custom_search_results');
  // end SEARCH FORM

?>

Here I hook into the response of my custom form and add:

  • The actual query ("query" and not "s"), when the user inputs valid data.
  • The filter by taxonomy, when needed.

I have no clue why this is not working out (spent a day!), I can see my query coming back with the correct 's' parameter:

$query->set('s', ij_get_str($_GET['query']));

but not the taxonomy filter.

What am I doing wrong? I want to use the standard Loop in my search.php file, so in some way I want my hook function to get automatically plugged into the loop. One curious thing is that if I create a new WP_Query object with the same setup (and not use the "set" method) it returns exactly what I want! I tried doing $query = new WP_Query... but this is not working too!!

If I dump the query request on the top of my search.php I get the following:

SELECT SQL_CALC_FOUND_ROWS  wp_posts.* FROM wp_posts  WHERE 1=1  AND 0 = 1 AND (((wp_posts.post_title LIKE '%vil%') OR (wp_posts.post_content LIKE '%vil%')))  AND wp_posts.post_type = 'immobiliare' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10

Does anyone have any hints to share? You would be really cool to give me a hand! Thanks Dan


回答1:


You probably solved this by now, but your query has ... where 1=1 and 0=1 ... so it wouldn't return anything since you are asking for a record set of items both true and false.



来源:https://stackoverflow.com/questions/10941810/wp-query-not-filtering-tax-query-correctly-in-pre-get-posts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!