我已经建立了一个wordpress网站,它几乎完全使用魔术字段(而不是默认的帖子等)。
然而,我现在正在尝试实现搜索功能,发现wordpress无法找到任何由Magic Fields创建的内容。
我已经更改了搜索以创建一个自定义的WP_Query,但仍然没有任何运气。例如,我有一个post_type为"project":
$searchValue = $_GET['s'];
$args = array(
'post_type' => 'project',
'posts_per_page' => -1,
'meta_value' => $searchValue,
'meta_key' => 'title'
);
$query = new WP_Query($args);
这不会返回任何结果。我哪里错了?
非常感谢!
我在魔术场和wordpress搜索方面也遇到了问题。标准的wordpress搜索只搜索邮箱内容。处理搜索魔术字段内容的方法是搜索postmeta。
$add_value = true;
$query_array = array();
$query_for_posts = "page_id=";
$search_guery = $_GET['s'];
$search_results = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."postmeta WHERE meta_value LIKE '%" . $search_guery ."%' ORDER BY post_id");
if(!empty($search_results))
{
foreach ($search_results as $search_result)
{
//loop through results
for($i=0;$i<sizeof($query_array);$i++)
{
//check if post id in the array
if($search_result->post_id == $query_array[$i])
$add_value = false;
}
if($add_value)
{
//add the post id to the array if not a duplicate
array_push($query_array, $search_result->post_id);
//also add id for WP_Query
$query_for_posts .= $search_result->post_id . ",";
}
$add_value = true;
}
}
然后显示结果。
if(!empty($query_array))
{
for($i=0;$i<sizeof($query_array);$i++)
{
//get post from array of ids
$post = get_page($query_array[$i]);
//make sure the post is published
if($post->post_status == 'publish')
echo '<h3><a href="'.get_permalink($post->ID).'">'.$post->post_title.'</a></h3>';
}
}
else
{
//tell the user there are no results
}
您也可以在WP_query中使用$query_for_posts变量。它的值page_id=1,3,7,9,23…搜索结果中的所有帖子id。