0
votes

I'm trying to implement search, which will look through custom taxonomy terms. I have custom type called "product", and custom taxonomy called "type". When I'm using wordpress standard form it works good except the fact, that it doesn't search in custom taxonomy terms.

So, what I need is: 1. Make WP search through "m_type" taxonomy terms 2. It should use "name" of the term, instead of "slug".

I was trying to include additional query vars to make it look through "slugs" at least.

This code:

$wp_query->query['tax_query'] =  array(
        array(
            'taxonomy' => 'm_type',
            'field' => 'slug',
            'terms' => 'pen'
        )
    );

Generates the following SQL:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships 
ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON 
(wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id 
IN (163) ) AND (((wp_posts.post_title LIKE '%pen%') OR (wp_posts.post_content LIKE 
'%pen%'))) AND wp_posts.post_type IN ('product') AND (wp_posts.post_status 
= 'publish' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private') AND
 (wp_postmeta.meta_key = 'max_price' ) GROUP BY wp_posts.ID 
ORDER BY wp_postmeta.meta_value+0 DESC LIMIT 0, 60

So, basically it adds custom taxonomy, search through posts, that have this kind of taxonomy (*wp_term_relationships.term_taxonomy_id IN (163)*) but actually never compares taxonomy term with query string.

Maybe I'm doing all of this wrong?

1
Did you get the answer ? If yes then Ill appreciate if youll upvote or select it as a right answer. :)Chirag Swadia

1 Answers

1
votes

I am using this sql query to fetch results on the basis of title/taxonomy term/content search where $search is the search parameter and $_REQUEST['post_type'] that I am p

                SELECT DISTINCT(ID) FROM 
                $wpdb->terms AS terms 
                JOIN
                $wpdb->term_taxonomy as termtaxonomy
                JOIN
                $wpdb->term_relationships AS termrelationship
                JOIN
                $wpdb->posts AS posts

                ON terms.term_id = termtaxonomy.term_id &&
                   termrelationship.term_taxonomy_id = termtaxonomy.term_taxonomy_id &&
                   termrelationship.object_id = posts.ID
                WHERE terms.name LIKE '%".$search."%' OR posts.post_title LIKE       '%".$search."%' OR posts.post_content LIKE '%".$search."%' AND posts.post_type='".$_REQUEST['post_type']