-1

我需要在 wordpress 中自定义搜索,因此搜索也会在 postmeta 表中查找关键字。我为 wp_query 变量放置了 meta_query 变量。wp_query 对象的查询输出如下:

SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  INNER JOIN wp_postmeta ON        (wp_posts.ID = wp_postmeta.post_id)

INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id) INNER JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id) WHERE 1=1 AND wp_posts.post_parent = 0 AND (((wp_posts.post_title LIKE '% 4422%') OR (wp_posts.post_content LIKE '%4422%'))) AND wp_posts.post_type = 'wpsc-product' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') AND ((wp_postmeta .meta_key = 'CAS #' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '%4422%') OR (mt1.meta_key = 'style' AND CAST(mt1.meta_value AS CHAR) LIKE '%4422%') OR ( mt2.meta_key = 'Packaging' AND CAST(mt2.meta_value AS CHAR) LIKE '%4422%') ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_title ASC LIMIT 0, 75

现在的问题是它同时在 post 和 postmeta 表中查找。因此,如果关键字不在 post_content 或 post_title 中并且不在元字段中,则什么都不会出现。

我需要它来查看 post_content、post_title 和元字段中的任何位置,以便在其中任何一个中找到值,它会显示结果。现在它正在显示

4

1 回答 1

0

试试下面的代码:

wp_reset_query();
$args = array('ignore_sticky_posts' => 1,'meta_key'=> KEY_TO_SEARCH, 'meta_value'=>$search_value, 'post_type'=> 'post','orderby' => 'post_date','order' => 'DESC','post_status' => 'publish',  'posts_per_page' => $post_to_show);
 query_posts($args);
于 2012-06-26T18:58:23.280 回答