1

I have two sets of posts - some have a Custom Field (true/false). So the ones which have a Custom Field (for this, let's just call it custom_field) value of true, go in one set, and the others (the ones with false, or more commonly, where the custom_field doesn't exist on that post) go in the other set.

When I search for something, I want to be able to show these two sets of posts separately from each other, i.e so the first set results (true) is displayed, and then the second set of results (false)). This all works fine, but I also need to be able to display the results count for each set. I have written a WP_Query which I thought would work:

$postsTrue = new WP_Query( array( "s" => get_search_query(), "meta_key" => "custom_field", "meta_value" => "true") );
$countTrue = $postsTrue->post_count;

So this one should be getting the number of results where the value of custom_field is true. However, it instead gets all the results (for both true and false).

How do I go about writing a query to only get the number of results where the value of custom_field is true?

  • Is the value of "custom_field" a string called "true" or an actual boolean value of true? – DrewAPicture Sep 23 '14 at 16:26
  • It's just a string value, written in the Custom Field editor of WordPress' Edit Post page. – Tom Oakley Sep 23 '14 at 16:30
0

Instead of $postsTrue->post_count;, try $postsTrue->found_posts; which returns the number of posts queried. $post_count counts all the posts being displayed.

Reference: codex

  • $postsTrue->found_posts; returns the same I'm afraid. If I do var_dump($postsTrue) it also returns all the posts being displayed, regardless of what value they have for the custom_field. – Tom Oakley Sep 24 '14 at 13:23
0

I think here your best friends are the meta_compare params on WP_Query. Have you tried those?

Also, just an idea but using meta_value_num might work if WP casts bool(true) as 1.

0

Like Mike said, my first thought is what your actual meta_value is. Whether it's stored as a string or it's a boolean value...because that'll definitely screw things up.

Secondly, I'd recommend using something like Advanced Custom Fields instead of using WordPress' vanilla fields. It makes it much harder for the user to mess up, because in cases like this can be handled with a dropdown or radio button; or even a tickbox (my preferred for your situation).

A tickbox will return a boolean, so you can do a meta_query that'll look for yes, then a query that'll look for not yes (that's no, OR empty).

Finally, a tiny personal preference...pass in WP_Query arguments as an array; makes it easier to read.

$YesArgs = array(
        's' => get_search_query(),
        'meta_query' => array(
          array(
             'key' => 'custom_field',
             'value' => 'yes'
          )
        )
    );

$postsTrue = new WP_Query($YesArgs);

$NoArgs = array(
        's' => get_search_query(),
        'meta_query' => array(
          array(
             'key' => 'custom_field',
             'value' => 'yes',
             'compare' => '!='
          )
        )
    );

$postsFalse = new WP_Query($NoArgs);
  • This should work, but I've edited my search parameters to search Categories/Tags as well as the Post Titles, which screws up the WordPress query and for some reason it ignores the Custom Field part of the Query. Once I get this fixed it will work. So thanks for helping me out with the meta_query syntax and stuff :) – Tom Oakley Sep 25 '14 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.