combine keyword search and tax query in wp_query

末鹿安然 提交于 2019-12-08 07:24:46

问题


I've been trying to create a custom search query, and I've made some progress on it, but have hit another roadbump.

I'm trying to combine the meta_query, keyword search ('s') and tax_query in a wp_query with an 'OR' relationship.

I've gotten the meta_query and 's' to work together thanks to this fantastic post: https://wordpress.stackexchange.com/questions/99849/search-that-will-look-in-custom-field-post-title-and-post-content

however, the tax_query is still giving me trouble. I tried adding it in via the same method, but it seems as though wordpress does some other magic with tax_query before it outputs it to the SQL query.

Here's what I've got thus far:

        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;

And this works. However, it doesn't include the search for the the Tags or Categories. I attempted to add it in manually via the posts_join and posts_search filter, but then I realized that wordpress is comparining values in the tax_query BEFORE the outputted SQL query, which causes problems when trying to add it in.

Any help would be greatly appreciated.

EDIT: for clarification, I'm trying to add in:

'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),
                )
            ),

but with an OR type relationship as opposed to the AND relationship wordpress adds by default.


回答1:


There are no way you can use tax_query for this purpose. You must override filter that wordpress provided to achieve this mission. Here is my code. Hope it help:

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

I just add posts_where and modify posts_join filter.



来源:https://stackoverflow.com/questions/29993312/combine-keyword-search-and-tax-query-in-wp-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!