Home Forums General Issues Order by true/false field using WP_Query


Order by true/false field using WP_Query

  • Hello,

    I’m a big of a noobie with ACF and am trying to achieve a simple sort of articles on a custom field. This field is of type True/False.

    I used the code from the documentation, but it does not work as expected.
    The result of using

    $args = array(
    	'meta_key'		=> 'complete',
    	'orderby'		=> 'meta_value_num',
    	'order'			=> 'DESC'
    $wp_query = new WP_Query( $args );

    is that only articles where the “complete” field is set to True are shown in the list at all.

    I wish however to show all articles, and just have the ones with “complete” set to False first, and then those where it’s set to true.

    I tried to tinker a bit with meta_query too, use orderby meta_value but no luck.

    Could you please help me ? Thanks a lot in advance.

  • Hi @istrasoft

    I think unfortunately, this is a limitation of the WP_Query object. For WP to order the posts, each post must have a value in the meta field ‘complete’. If no value is found, the SQL won’t run correctly for that post.

    Perhaps there are some resources on Google that show how to use a default meta value for posts that don’t have a value?


  • Hi @elliot and thanks for the quick reply.

    Does this mean that it is impossible to order by a true/false ACF field at all, when using WP_Query ?

    When the checkbox for that field is unchecked, does ACF save 0 or a NULL into the database ?

    Is there a way to define a default value so there are no NULLs in database ?

    I’m quite surprised because when you run a query against a table and order by a field, even records with NULL are ordered as expected..

  • Hi @istrasoft

    I would need to run some tests to give you the correct information, but off the top of my head, I’m not 100% sure about the WP_Query / Null issue.

    If you save a post with a checkbox unticked, it will save the value 0.

    I agree that the WP_Query should be able to treat no value as a 0 and order the posts correctly.
    Perhaps this will require some googleing / debugging your SQL result (from the WP_Query object) to find out why the SQL is not returning the correct results

    Good luck

  • Hi @elliot and thanks for your information.

    I investigated and here are the results. They may be useful to a future person that has the same issue, or maybe to enhance ACF a bit.

    I switched the field type from True/False to Radio, where I was able to provide two text values ‘yes’ / ‘no’ and defined ‘no’ to be the default value.

    However, the WP_Query still only returned the same, and then it hit me. The problem was not what I thought. Actually, I had already 250 articles, and when adding a new custom field through ACF, unless each article is manually edited and updated, the custom field is not listed at all in the meta data of that article. This does not depend on the actal value or default value of the field.

    So, what would be a nice to have feature is that when adding a new field, having a button or some action link that would allow to set the default value for this new custom field, on all items using this field set, and which have no value for that field at all. Also, adding a little note about this behaviour into the documentation on the queries would help the newbie user to realize how it works.

    Thanks 🙂

  • Hi @istrasoft

    Great feedback. Yes. I will look into this and perhaps find a way to create a ‘default’ WP_Query value to solve the issue perfectly.

    I know you can hook into the SQL sort statement and write a CASE where you can define values that don’t exist, but I will need to trial this code before mentioning it.


  • if anyone is searching for a solution to this issue, i had 2000 posts and found a way.

    just write a php script that loads wp (outside of wp theme folder).
    it should first go through groups of posts if you have to many (say grouping of 50). i did it with a url start stop var ?start=0&stop=100

    you can first load the post, take the title into a var

    then update the post using wp_update_post and the title from the var

    it took about 10 mins to update all 2000 after each load and changing the start stop and because i am force saving the post, the acf default values for some fields were generated and saved.

    plus this was with wpml translated posts as well, so it was about 3800 as some translations were missing.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Order by true/false field using WP_Query’ is closed to new replies.