在wp_query中结合关键字搜索和税务查询

时间:2015-05-01 19:00:27

标签: php sql wordpress wp-query

我一直在尝试创建一个自定义搜索查询,我已经取得了一些进展,但已经遇到了另一个道路爆炸。

我正在尝试将w__query中的meta_query,关键字搜索('s')和tax_query与'OR'关系结合起来。

由于这篇精彩的文章,我得到了meta_query和's'一起工作: https://wordpress.stackexchange.com/questions/99849/search-that-will-look-in-custom-field-post-title-and-post-content

然而,tax_query仍然给我带来麻烦。我尝试通过相同的方法添加它,但似乎wordpress在将其输出到SQL查询之前使用tax_query执行其他一些魔法。

这是我到目前为止所得到的:

        function add_join_wpse_news($joins) 
        {  
            global $wpdb;
            return $joins . " INNER JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)" ;
        }
        function alter_search_wpse_news($search,$qry)
        {
            global $wpdb;
            $add = $wpdb->prepare("({$wpdb->postmeta}.meta_key = '_et_builder_settings' AND CAST({$wpdb->postmeta}.meta_value AS CHAR) LIKE '%%%s%%')",$qry->get('s'));
            $pat = '|\(\((.+)\)\)|';
            $search = preg_replace($pat,'(($1 OR '.$add.'))',$search);
            return $search;
        }
        function alter_groupby_wpse_news($groupby)
        {
            global $wpdb;

            $mygroupby = "{$wpdb->posts}.ID";
            if( preg_match( "/$mygroupby/", $groupby )) {
                // grouping we need is already there
                return $groupby;
            }

            if( !strlen(trim($groupby))) {
                // groupby was empty, use ours
                return $mygroupby;
            }

            // wasn't empty, append ours
            return $groupby . ", " . $mygroupby;
        }
        add_filter('posts_join','add_join_wpse_news');
        add_filter('posts_search','alter_search_wpse_news',1,2);
        add_filter('posts_groupby', 'alter_groupby_wpse_news' );

        $args_condensed = array
        (
            'post_type' => 'news',
            'paged' => $paged,
            's' => $getname,
        );
        $the_query = new WP_Query($args_condensed);
        $max_pages = $the_query->max_num_pages;
        echo $GLOBALS['the_query']->request;

这很有效。但是,它不包括搜索标签或类别。我尝试通过posts_join和posts_search过滤器手动添加它,但后来我意识到wordpress在输出的SQL查询之前比较tax_query中的值,这在尝试添加它时会导致问题。

非常感谢任何帮助。

编辑:为了澄清,我正在尝试添加:

'tax_query' => array
            (
                'relation' => 'OR',
                array //Search Tag
                (
                    'taxonomy' => 'post_tag',
                    'field' => 'slug',
                    'terms' => array($getname)
                ),
                array //Search Category
                (
                    'taxonomy' => 'category',
                    'field' => 'slug',
                    'terms' => array($getname),
                ),
                array //Search Category (Single Words)
                (
                    'taxonomy' => 'category',
                    'field' => 'slug',
                    'terms' => explode(" ",$getname),
                ),
                array //Search Tag (Single Words)
                (
                    'taxonomy' => 'post_tag',
                    'field' => 'slug',
                    'terms' => explode(" ",$getname),
                )
            ),

但是OR类型关系与AND关系相反,wordpress默认添加。

1 个答案:

答案 0 :(得分:4)

您无法将tax_query用于此目的。您必须覆盖wordpress提供的过滤器才能完成此任务。这是我的代码。希望它有所帮助:

    function add_join_wpse_news($joins)
    {
        global $wpdb;
        $joins = $joins . " INNER JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)" ;
        $joins .= " inner join  {$wpdb->term_relationships}  as wrel on {$wpdb->posts}.ID = wrel.object_id";
        $joins .= " inner join {$wpdb->term_taxonomy} as wtax on wtax.term_taxonomy_id = wrel.term_taxonomy_id";
        $joins .= " inner join {$wpdb->terms} as wter on wter.term_id = wtax.term_id";

        return $joins;

    }
    function add_where_wpse_news($where) {
        $getname = 'what you want';
        return $where. ' AND '. "wter.slug like '%$getname%' ";
    }
    add_filter('posts_join','add_join_wpse_news');
    add_filter('posts_where','add_where_wpse_news');

我只需添加posts_where并修改posts_join过滤器。