Search result count not matching actual result

I have a custom search in place where I've excluded posts that have a custom field with a specific value, but the search count is including those with the custom field value. How can I get the search count to match the actual number of results?

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;

EDIT: I tried since to get the count with a sql statement. The results are always the same number of posts no matter what search keyword I use. How can I get the count to be accurate and exclude the custom field that has a certain value?

$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>";

Solutions

If you want to exclude certain posts, wouldn't the conditional be:

 if ($value != '' &&  get_post_meta($post->ID, 'value', true) != 'value_to_exclude')

Anyhow, you're excluding AFTER you run the query. So $total_results will always be 'wrong'. The better approach is to write sql for WP_Query that does the excluding for you. But you didn't include the query, so that cannot be addressed.

Similar questions

Selecting most recent result from MySQL where there are more than 1 rows matching ID
I'm using wordpress with some custom fields, to cut this short, im echoing out all rows that have the meta_key of post_author that arent empty and grouping likewise names together otherwise i might get Pete, Steve, Pete - when all i need is Pete, Steve. Below is the SQL that makes that work That works great. But what if i change the author FROM pet...
Why is this NULL result matching my if statement?
This is my if statement. Pretty straightforward: This is what I'm doing to test is_page(): So everything works okay. But when $pagename is NULL the first if statement passes. I have no idea why. What's wrong and what's the solution?
Search format not matching taxonomy query
I have inherited some code in this project. This search works. I can click on each individual taxonomy and see the results displayed on my page. However, I would like this to extend the search rather than just perform a new search i.e. I am trying to find an event in a region and the category of event and the age group. ?region=102&category=8&a...
How can I show the actual "search term" from the content in the post page and display it in wordpress?
I want to show on the search results page, a sentence or few words where the search term is actually located within the content in the post and display it and underline the search term. For the title I have done similar like this: How can I show approx 15 words or so from the actual "search term" from the post content page and display it? for examp...
Display Search Result Count
Until now I have been using below code to get the number of results when someone searches and to display that count. But this does not seem like valid code. It shows below error: Deprecated: Assigning the return value of new by reference is deprecated Can anyone please suggest the proper way in which I get get the search count. The above code is pl...
Get search result count in plugin
Is it possible to get count of search results in plugin? I tried to use pre_get_posts action, but post_count always returned 0. I need to know if there are any search results and if not, then do something else. I know I can edit search.php, but is it possible to make this using plugin?

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .