最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Wordpress tax query use operator LIKE

programmeradmin2浏览0评论

I have taxonomy color which is associated with a custom post type. I am listing all the post meta and the taxonomy in a table. I have a option in the table to search the posts matching the search value.

when search key is entered it will do a ajax call to get the the posts.

this is the query to get all the posts matching the search string.

function get_query_posts_custom($post_id,$start,$length)  {
    //get_posts arguments
    $args =  array(
        'post_type'     => 'custom_post',
        'post_status'   => array('publish'),
        'numberposts'   => $length,
        'offset'        => $start,
        'orderby'       => 'menu_order',
        'order'         => 'asc',
        'post_parent'   => $_product->id
    );


    //get custom post taxonomy
    $taxonomies = (array) maybe_unserialize(get_post_meta( $post_id, 'post_taxonomy', true));
    if(empty($attributes)) {
        $taxonomies = array();
    }

    $meta_keys  = array('type','code','key');       
    if(!empty($search)) {
        foreach($meta_keys as $meta_key) {
            $meta_query[] = array(
                        'key'   => $meta_key,
                        'value' => $search,
                        'compare'   => 'LIKE';
                    );

        }

        $args['meta_query'] = $meta_query;
        $tax_query = array();

        foreach($taxonomies as $taxonomy) {
            $tax_query  = array(
                'taxonomy'  =>  $taxonomy;
                'field' =>  'slug';
                'terms' =>  $search;
                'operator'  => 'LIKE';
            );
        }
        if(count($tax_query)) {
            $args['tax_query'] = $tax_query;
        }
    }
    $results    = get_posts($args);
            return $results;
}

How to get the posts that matches the search string of a taxonomy?

I searched the wordpress function reference it says only operator allowed to tax_query are 'IN,NOT IN, OR and AND') can I use the LIKE operator?

I have taxonomy color which is associated with a custom post type. I am listing all the post meta and the taxonomy in a table. I have a option in the table to search the posts matching the search value.

when search key is entered it will do a ajax call to get the the posts.

this is the query to get all the posts matching the search string.

function get_query_posts_custom($post_id,$start,$length)  {
    //get_posts arguments
    $args =  array(
        'post_type'     => 'custom_post',
        'post_status'   => array('publish'),
        'numberposts'   => $length,
        'offset'        => $start,
        'orderby'       => 'menu_order',
        'order'         => 'asc',
        'post_parent'   => $_product->id
    );


    //get custom post taxonomy
    $taxonomies = (array) maybe_unserialize(get_post_meta( $post_id, 'post_taxonomy', true));
    if(empty($attributes)) {
        $taxonomies = array();
    }

    $meta_keys  = array('type','code','key');       
    if(!empty($search)) {
        foreach($meta_keys as $meta_key) {
            $meta_query[] = array(
                        'key'   => $meta_key,
                        'value' => $search,
                        'compare'   => 'LIKE';
                    );

        }

        $args['meta_query'] = $meta_query;
        $tax_query = array();

        foreach($taxonomies as $taxonomy) {
            $tax_query  = array(
                'taxonomy'  =>  $taxonomy;
                'field' =>  'slug';
                'terms' =>  $search;
                'operator'  => 'LIKE';
            );
        }
        if(count($tax_query)) {
            $args['tax_query'] = $tax_query;
        }
    }
    $results    = get_posts($args);
            return $results;
}

How to get the posts that matches the search string of a taxonomy?

I searched the wordpress function reference it says only operator allowed to tax_query are 'IN,NOT IN, OR and AND') can I use the LIKE operator?

Share Improve this question asked Feb 7, 2014 at 5:23 B L PraveenB L Praveen 1551 gold badge2 silver badges14 bronze badges 3
  • Have you tried the solution in this thread: wordpress.stackexchange/questions/123298/…? – 1fixdotio Commented Feb 7, 2014 at 7:50
  • I tried to get the term ID matching the search using the get_terms function with name_like arugment and generate the tax_query with IN operator. – B L Praveen Commented Feb 7, 2014 at 10:07
  • Does the get_posts support the operator argument in the tax_query. Function Reference does not mention anything about the tax_query with operator. Only in WP_Query it is explained. – B L Praveen Commented Feb 7, 2014 at 10:11
Add a comment  | 

4 Answers 4

Reset to default 5

The only option you have is to write your own SQL into the posts_clauses filter, where you get an array of the JOIN, WHERE, ORDER, etc. clauses that you can alter, add to, remove, etc.

One MAJOR MAJOR note on this, is ALWAYS use the global $wpdb's prepare function, which will sanitize all your lovely datas. You don't want to be allowing any type of injection through your search term custom queries. :)

As stated in other answers you cannot simply do a LIKE-wise search using tax_query.

What you can do is either altering the SQL statement using filters as suggested by @Eric Holmes which is an advanced technique. You need to know what you are doing. Or you could just make a separate query loading the taxonomy terms first (using LIKE) and then loading the actual posts.

Here is a simple sample for loading posts that are in relation to any terms matching a LIKE compare against the $search variable.

//  load the terms using LIKE
$termIds = get_terms([
    'name__like' => $search,
    'fields' => 'ids'
]);


//  load the posts using the found term IDs
$posts = get_posts([
    'tax_query' => [
        'relation' => 'OR',
        [
            'taxonomy' => 'yourtaxonomy',
            'field' => 'id',
            'terms' => $termIds,
        ],
        [
            'taxonomy' => 'othertaxonomy',
            'field' => 'id',
            'terms' => $termIds,
        ],
    ],
]);

WP_Tax_Query respectively the Taxonomy Parameter of WP_Query or get_posts() does only take the following for the operator argument:

'operator' string (optional)
Possible values: 'AND', 'IN' or 'NOT IN'.
Default: 'IN'

So you can not use LIKE as operator.

Actually you don't need to use compare arguments/operators in taxonomy queries (but in meta queries, yes.)

so, this modified part should behave as using 'LIKE' by default:

foreach($taxonomies as $taxonomy) {
            $tax_query  = array(
                'taxonomy'  =>  $taxonomy;
                'field' =>  'slug';
                'terms' =>  $search;
            );
        }
发布评论

评论列表(0)

  1. 暂无评论