Sql Wordpress自定义分类搜索

Sql Wordpress自定义分类搜索,sql,wordpress,Sql,Wordpress,我正在尝试实现搜索,它将查看自定义分类术语。 我有称为“产品”的自定义类型和称为“类型”的自定义分类法。当我使用wordpress标准表单时,除了不在自定义分类术语中搜索之外,它工作得很好 SELECT DISTINCT(ID) FROM $wpdb->terms AS terms JOIN $wpdb->term_taxonomy as termtax

我正在尝试实现搜索,它将查看自定义分类术语。 我有称为“产品”的自定义类型和称为“类型”的自定义分类法。当我使用wordpress标准表单时,除了不在自定义分类术语中搜索之外,它工作得很好

                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']
所以,我需要的是: 1.通过“m_类型”分类术语进行WP搜索 2.它应该使用术语的“名称”,而不是“slug”

                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']
我试图包括额外的查询变量,使它至少能看穿“slug”

                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']
此代码:

$wp_query->query['tax_query'] =  array(
        array(
            'taxonomy' => 'm_type',
            'field' => 'slug',
            'terms' => 'pen'
        )
    );
                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']
生成以下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
                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']
因此,基本上,它添加了自定义分类法,即通过帖子搜索,这种分类法(*wp_term_relationships.term_taxonomy_id IN(163)*),但实际上从未将分类法术语与查询字符串进行比较

                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']

也许我都做错了

我使用此sql查询获取基于标题/分类术语/内容搜索的结果,其中$search是搜索参数,$\u REQUEST['post\u type'],我是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']

你得到答案了吗?如果是,我将感谢您投票或选择它作为正确答案。(