wp_query无法在pre_get_posts中正确过滤tax_query [英] wp_query not filtering tax_query correctly in pre_get_posts

查看:99
本文介绍了wp_query无法在pre_get_posts中正确过滤tax_query的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下情况的问题:我正在创建一个自定义表单来搜索我的自定义帖子类型(不动产),并且与此相关,我有2个自定义分类法:位置和提示。

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.

我的searchform.php是:

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>

如您所见,我有3个字段:1是文本字段,其他2是带有下拉菜单我的分类法。我希望用户能够搜索这些字段。我不得不修改 s输入类型,因为它不允许我提交空查询,而是添加了 query字段。
我的functions.php文件如下:

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:


  • 当用户输入有效数据时的实际查询(查询而不是 s)。

  • 按分类法过滤,当需要时。

我不知道为什么这行不通(花了一天!),我可以看到查询即将到来返回正确的's'参数:

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']));

但不是分类过滤器。

我究竟做错了什么?我想在我的search.php文件中使用标准的Loop,所以我希望以某种方式将钩子函数自动插入到循环中。奇怪的是,如果我使用相同的设置创建一个新的WP_Query对象(而不使用 set方法),它将返回我想要的结果!我尝试做$ query = new WP_Query ...但这也行不通!

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!!

如果我将查询请求转储到search.php的顶部,我会得到以下内容:

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 = 1和0 = 1 ...因此它不会返回任何内容,因为您要查询记录集true和false。

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.

这篇关于wp_query无法在pre_get_posts中正确过滤tax_query的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆