WP_QUERY无法识别自定义搜索中的分类参数

时间:2016-08-02 作者:benjosity

我有一个带有一个标准文本输入和一个选择输入的搜索表单,用于选择分类术语。

查询使用输入运行WP\\U查询并显示结果。唯一的问题是,查询似乎没有运行taxonomy参数。我可以很好地输入关键字,它将显示正确的结果,但它不会拾取分类参数。如果我没有输入任何关键字,但从选择输入中选择了一个分类术语,则找不到任何结果。

选择输入中的值正在传递,它位于搜索结果页面的URL中,我已将其回显到结果页面上。

当我回显WP\\u查询请求时,它不会详细说明在自定义分类法“product category”中查找产品的任何尝试。

这是表格-

<form method="get" id="advanced-searchform" role="search" action="<?php echo esc_url( home_url( \'/\' ) ); ?>">
    <input type="text" name="product-name" value="" placeholder="Enter product name or code">
    <input type="submit" value="Search">

    <select name="category">
        <option value="" disabled selected>Select category</option>

      <?php
        $post_type = \'product\';
        $taxonomies = get_object_taxonomies( (object) array( \'post_type\' => $post_type, \'order\' => \'ASC\', \'orderby\' => \'date\' ) );

        foreach( $taxonomies as $taxonomy ) :
            $terms = get_terms( $taxonomy );

            foreach( $terms as $term ) :
      ?>

        <option value="<?php echo $term->slug; ?>"><?php echo $term->name; ?></option>
      <?php
            endforeach;
        endforeach;
      ?>

    </select>

    <input class="hidden-search" type="hidden" name="search" value="advanced">
</form>
这里是查询-

$_keyword   = $_GET[\'product-name\'];
$_cat       = $_GET[\'category\'];

$product_args = array(
    \'s\'              => $_keyword,
    \'post_type\'      => \'product\',
    \'posts_per_page\' => 10,
    \'taxonomy\'       => \'product-category\',
    \'terms\'          => $_cat
);

$productSearchQuery = new WP_Query( $product_args );

echo $productSearchQuery->request;
也尝试过-

$product_args = array(
    \'s\'              => $_keyword,
    \'post_type\'      => \'product\',
    \'posts_per_page\' => 10,
    \'tax_query\'      => array( array(
        \'taxonomy\' => \'product-category\',
        \'field\'    => \'slug\',
        \'terms\'    => $_cat,
    ) ),
);
查询请求示例-

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID 
FROM wp_posts 
WHERE 1=1 
    AND ( 0 = 1 ) 
    AND wp_posts.post_type = \'product\' 
    AND (wp_posts.post_status = \'publish\' 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

1 个回复
最合适的回答,由SO网友:Howdy_McGee 整理而成

更好的选择可能是使用pre_get_posts 钩子改为在请求查询之前修改查询,而不是创建自己的查询。下面是它的样子:

/**
 * Modify Theme Queries
 * - https://developer.wordpress.org/reference/hooks/pre_get_posts/
 * - https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
 *
 * @param $query
 *
 * @return void
 */
function theme_pgp( $query ) {

    if( is_admin() ) {
        return;                 // If we\'re in the admin panel - drop out
    }

    if( $query->is_main_query() && $query->is_search() ) {      // Apply to all search queries

        // IF our category is set and not empty - include it in the query
        if( isset( $_GET[\'category\'] ) && ! empty( $_GET[\'category\'] ) ) {
            $query->set( \'tax_query\', array( array(
                \'taxonomy\'  => \'product-category\',
                \'field\'     => \'slug\',
                \'terms\'     => array( sanitize_text_field( $_GET[\'category\'] ) ),
            ) ) );
        }
    }
}
add_action( \'pre_get_posts\', \'theme_pgp\' );
此时,您可以在搜索页面上使用标准循环,而不是自定义WP\\U查询:

if( have_posts() ) {
    while( have_posts() ) {
        the_post();

        the_title();
    }
}

相关推荐

在wp-json/wp/v2/Search中添加帖子字段

我正在尝试在wp-json/wp/v2/search 获取端点,但此端点似乎没有考虑register_rest_field 方法add_action(\'rest_api_init\', function() { register_rest_field(\'post\', \'excerpt\', array( \'get_callback\' => function ($post_arr) { die(var_dump($post_arr)