无法使用WP_QUERY将“Search Value”和“Tax_Query”组合在一起

时间:2013-07-16 作者:Derfder

这是我在模板函数中的函数。php。

问题是这是可行的:

function ajax_search_action_do(){
    global $wp_query;

    $search = $_POST[\'search_value\'];
    $country   = $_POST[\'country_value\'];

    $args = array(
        \'s\' => $search,
        \'posts_per_page\' => 10,
    );
    $wp_query = new WP_Query( $args );

    get_template_part( \'search-results\' );

    exit;
}
但这不是:

function ajax_search_action_do(){
    global $wp_query;

    $search = $_POST[\'search_value\'];
    $country   = $_POST[\'country_value\'];



    $args = array(
        \'s\' => $search,
        \'posts_per_page\' => 10,
        \'tax_query\' => array(                
          array(
            \'taxonomy\' => \'country\',               
            \'field\' => \'id\',                      
            \'terms\' => $country,   
          ),
        ),
    );
    $wp_query = new WP_Query( $args );

    get_template_part( \'search-results\' );

    exit;
}
所以,当只有搜索词时,它就起作用了,我得到了正确的结果。但是,当我尝试将其与tax\\u查询结合使用时,搜索词不起作用,只有tax\\u查询起作用。

如何使这两个条件一起工作?

生成的查询:

`SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (8) ) AND (((wp_posts.post_title LIKE \'%test%\') OR (wp_posts.post_content LIKE \'%test%\'))) AND wp_posts.post_type IN (\'post\', \'page\', \'attachment\') AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'future\' OR wp_posts.post_status = \'draft\' OR wp_posts.post_status = \'pending\' OR wp_posts.post_author = 1 AND wp_posts.post_status = \'private\') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10`

3 个回复
SO网友:Kevin Leary

这是一个有问题的GROUP BY 在以下情况下添加到WP\\U查询的子句tax_query 存在。它与s 参数您可以删除GROUP BY 完全是为了修复它,但您不应该对每个WP\\u查询都这样做。我经常使用以下课程来解决这个问题:

/**
 * Search Within a Taxonomy
 * 
 * Support search with tax_query args
 *
 * $query = new WP_Query( array(
 *  \'search_tax_query\' => true,
 *  \'s\' => $keywords,
 *  \'tax_query\' => array( array(
 *      \'taxonomy\' => \'country\',               
 *      \'field\' => \'id\',                      
 *      \'terms\' => $country,   
 *  ) ),
 * ) );
 */
class WP_Query_Taxonomy_Search {
    public function __construct() {
        add_action( \'pre_get_posts\', array( $this, \'pre_get_posts\' ) );
    }

    public function pre_get_posts( $q ) {
        if ( is_admin() ) return;

        $wp_query_search_tax_query = filter_var( 
            $q->get( \'search_tax_query\' ), 
            FILTER_VALIDATE_BOOLEAN 
        );

        // WP_Query has \'tax_query\', \'s\' and custom \'search_tax_query\' argument passed
        if ( $wp_query_search_tax_query && $q->get( \'tax_query\' ) && $q->get( \'s\' ) ) {
            add_filter( \'posts_groupby\', array( $this, \'posts_groupby\' ), 10, 1 );
        }
    }

    public function posts_groupby( $groupby ) {
        return \'\';
    }
}

new WP_Query_Taxonomy_Search();
现在您可以添加自定义search_tax_query 要将此功能添加到的WP\\u查询对象。例如:

$query = new WP_Query( array(
    \'search_tax_query\' => true,
    \'s\' => $keywords,
    \'tax_query\' => array( array(
        \'taxonomy\' => \'country\',               
        \'field\' => \'id\',                      
        \'terms\' => $country,   
    ) ),
) );
为了解释这里发生了什么,问题是由GROUP BY 使用tax_query 论点当您将此与搜索相结合时,我们不需要/不需要此子句。如果删除它,搜索结果将按预期工作:匹配搜索查询的帖子(s) 在分类法中(tax_query).

SO网友:Kelvin

我正在尝试创建一个wordpress自定义搜索this post.

试试这个?

function ajax_search_action_do(){
    global $wp_query;

    $search = $_POST[\'search_value\'];
    $country   = $_POST[\'country_value\'];

    $args = array(
        \'s\' => $search,
        \'posts_per_page\' => 10,
        \'tax_query\' => array(
          \'relation\' => \'OR\',                
          array(
            \'taxonomy\' => \'country\',               
            \'field\' => \'id\',                      
            \'terms\' => $country,   
          ),
        ),
    );
    $wp_query = new WP_Query( $args );

    get_template_part( \'search-results\' );

    exit;
}

SO网友:Mat_

我认为您可能正在init 行动挂钩。但是如果是这样的话,init 在AJAX调用期间不会调用。

要完成此操作,您可以通过示例手动运行init操作,如下所示:

function ajax_search_action_do(){
   do_action(\'init\'); 
   global $wp_query;
   ...
}

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post