WordPress tax query use operator LIKE

Question

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?

0
B L Praveen 4 years 2020-03-28T08:50:53-05:00 0 Answers 103 views 0

Leave an answer

Browse
Browse