如果你不想用SQL做到這一點,這就是我做我的搜索頁面。
基本問題:當進行meta_query時,wordpress認爲我希望條件以「AND」而不是「OR」加入。
所以Wordpress尋找標題/內容=「myContent」和aioseop_keyword「myContent」的頁面。這(在我的情況下)導致零結果,儘管有一個與SEO關鍵字匹配的頁面。
爲了解決這個問題,我做了兩個查詢。聽起來很簡單,但是:儘管在$ post對象中有帖子,但Loop不想識別帖子。在看過the have_posts() function後我發現了這個解決方案:它引用了其他變量而不僅僅是$ post對象。
$term = get_search_query(); // same as $_GET['s']
# the normal search:
$wordpress_keyword_search =& new WP_Query(array(
's' => $term,
'showposts' => -1
));
# now push already found post IDs to an array, so we can exclude them from the meta search.
foreach ($wordpress_keyword_search->posts as $post_)
$exclusion[] = $post_->ID;
# now do the meta query search
$aioseop_keyword_search =& new WP_Query(array(
'post__not_in' => $exclusion,
'post_type' => 'any',
'showposts' => -1,
'meta_query' => array(
array(
'key' => '_aioseop_keywords',
'value' => $term,
'compare' => 'LIKE',
)
)
));
# merge the two array posts.
# post_count and found_posts must be added together also.
# otherwise have_posts() returns false.
# see: http://core.trac.wordpress.org/browser/tags/3.6.1/wp-includes/query.php#L2886
$wordpress_keyword_search->posts = array_merge($wordpress_keyword_search->posts, $aioseop_keyword_search->posts);
$wordpress_keyword_search->found_posts = $wordpress_keyword_search->found_posts + $aioseop_keyword_search->found_posts;
$wordpress_keyword_search->post_count = $wordpress_keyword_search->post_count + $aioseop_keyword_search->post_count;
然後在一個簡單的循環使用:
if ($wordpress_keyword_search->have_posts()) {
while($wordpress_keyword_search->have_posts()) {
$wordpress_keyword_search->the_post();
# now you simply can:
the_title();
the_content();
}
} else {
echo '<p>Sorry, no posts found</p>';
}
我發現我的解決方案這個時候使用setup_postdata作爲SQL語法分析程序(見我自己的答案)。您的替代方案可能對未來有所幫助。感謝您給我一個替代解決方案! – 2009-08-30 09:07:34