用于TAX_QUERY和关键字的WP_QUERY OR子句

时间:2014-12-31 作者:tyteen4a03

是否可以在tax_query 和关键字?我想要WP_Query 如果存在tax_query 匹配或as 关键字匹配。

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

这里有一个小实验:

您可以尝试以下设置:

$args = array(
    \'wpse_search_or_tax_query\'      => true,    // <-- New parameter!
    \'s\'                             => \'some search text\',
    \'tax_query\'                     => array(
        array(
            \'taxonomy\' => \'category\',
            \'field\'    => \'slug\',
            \'terms\'    => array( \'some-category-slug\' ),
            \'operator\' => \'IN\',
        ),
    ),      
);
$query = new WP_Query( $args );
在那里我们介绍了风俗wpse_search_or_tax_query 参数以激活查询修改。

以下演示插件支持此操作:

<?php
/**
 * Plugin Name: Modify the WP_Query to use OR between the search and tax query parts.
 * Description: Activation through the boolean \'wpse_search_or_tax_query\' parameter.
 * Plugin URI:  http://wordpress.stackexchange.com/a/174221/26350
 * Author:      Birgir Erlendsson (birgire)
 * Version:     0.0.2
 */

add_action( \'init\', function()
{   
    if( ! is_admin() && class_exists( \'WPSE_Modify_Query\' ) )
    {
        $o = new WPSE_Modify_Query;
        $o->activate();
    }
});


class WPSE_Modify_Query
{
    private $search    = \'\';

    public function activate()
    {
        add_action( \'pre_get_posts\', array( $this, \'pre_get_posts\' ) );
    }

    public function pre_get_posts( WP_Query $q )
    {
        if( filter_var( 
                $q->get( \'wpse_search_or_tax_query\' ), 
                FILTER_VALIDATE_BOOLEAN 
             ) 
             && $q->get( \'tax_query\' ) 
             && $q->get( \'s\' )
        )
        {                                                           
            add_filter( \'posts_clauses\', array( $this, \'posts_clauses\' ), 10, 2 );
            add_filter( \'posts_search\', array( $this, \'posts_search\' ), 10, 2 );
        }
    }

    public function posts_clauses( $clauses, \\WP_Query $q )
    {
        remove_filter( current_filter(), array( $this, __FUNCTION__ ) );

        // Generate the tax query:
        $tq = new WP_Tax_Query( $q->query_vars[\'tax_query\'] );

        // Get the generated taxonomy clauses:
        global $wpdb;
        $tc = $tq->get_sql( $wpdb->posts, \'ID\' );

        // Remove the search part:
        $clauses[\'where\'] = str_ireplace( 
            $this->search, 
            \' \', 
            $clauses[\'where\'] 
        );

        // Remove the taxonomy part:
        $clauses[\'where\'] = str_ireplace( 
            $tc[\'where\'], 
            \' \', 
            $clauses[\'where\'] 
        );

        // Add the search OR taxonomy part:
        $clauses[\'where\'] .= sprintf( 
            " AND ( ( 1=1 %s ) OR ( 1=1 %s ) ) ", 
            $tc[\'where\'],
            $this->search
        );

        return $clauses;
    }

    public function posts_search( $search, \\WP_Query $q )
    {
        remove_filter( current_filter(), array( $this, __FUNCTION__ ) );
        $this->search = $search;
        return $search;
    }

} // end class
希望您可以根据自己的需要进一步调整。

结束

相关推荐

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

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