Wordpress自定义分类搜索

时间:2013-10-29 22:08:31

标签: sql wordpress

我正在尝试实施搜索,它将查看自定义分类术语。 我有自定义类型称为“产品”,自定义分类称为“类型”。当我使用wordpress标准表单时,它的效果很好,除了事实,它不会搜索自定义分类术语。

所以,我需要的是: 1.通过“m_type”分类术语进行WP搜索 2.它应该使用该术语的“名称”,而不是“slug”。

我试图包含额外的查询变量,以使其至少看看“slugs”。

此代码:

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

生成以下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

所以,基本上它增加了自定义分类,搜索帖子,具有这种分类法(* wp_term_relationships.term_taxonomy_id IN(163)*),但实际上从未将分类术语与查询字符串进行比较。

也许我在做这一切都错了?

1 个答案:

答案 0 :(得分:1)

我正在使用这个SQL查询来获取基于标题/分类术语/内容搜索的结果,其中$ search是搜索参数,$ _REQUEST ['post_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']