我有一个自定义搜索,其中我排除了具有特定值的自定义字段的帖子,但搜索计数包括具有自定义字段值的帖子。如何获得与实际结果数匹配的搜索计数?
global $wp_query;
$total_results = $wp_query->found_posts;
if ($total_results > 1) :
echo \'<h5>Search Results for "\'. get_search_query( false ) . \'": \' .\'<span class="results">\'.$ total_results.\'</span>\';
if( have_posts() ) :
while( have_posts() ) : the_post();
$value = get_post_meta($post->ID, \'value\', true);
if ($value != \'\' && get_post_meta($post->ID, \'value\', true) == \'value_to_exclude\') :
the_title();
endif;
endwhile;
endif;
编辑:从那以后,我尝试使用sql语句获取计数。无论我使用什么搜索关键字,结果总是相同数量的帖子。如何使计数准确并排除具有特定值的自定义字段?
$total_results =
"SELECT count(DISTINCT pm.post_id)
FROM $wpdb->postmeta pm
JOIN $wpdb->posts p ON (p.ID = pm.post_id)
WHERE pm.meta_key = \'approval\'
AND pm.meta_value = \'1\'
AND p.post_type = \'myposttype\'
AND p.post_status = \'publish\'
";
$count = $wpdb->get_var($total_results);
echo "<p>Count is: $count</p>";