The Problem
I’m having an issue with the Bricks Query Loop when sorting posts by a custom field date. Posts that don’t have a value for this date field (NULL values) are completely disappearing from my results instead of appearing at the end of the list.
My setup:
-
Post type: “candidatura”
-
Using ACF for custom fields
-
Sorting by custom field: “campos_gerais_data_de_submissao_da_candidatura” (submission date)
-
Some posts don’t have this acf custom field filled in yet. Many of this posts will only have this field after the user updates the post.
Loop without the ordering shows this:
Loop with the ordering show this:
What I’ve Tried
-
When I remove the ordering by the ‘campos_gerais_data_de_submissao_da_candidatura’, ALL posts show up correctly
-
When I add the ordering (either ASC or DESC), all the posts that have a value in the ‘campos_gerais_data_de_submissao_da_candidatura’ appear and also some posts appear that don’t have any value in the field. (This is strange for me)
About this issue I’ve asked claude ai and he says the following:
This is a classic issue with meta queries and ordering. The problem occurs because ordering by a meta field can implicitly filter out posts where that field doesn’t exist in the database at all (not even as NULL).
For posts with “Rascunho” status, it’s likely that the ‘campos_gerais_data_de_submissao_da_candidatura’ field isn’t even created in the database for many of them - it’s not just NULL, it simply doesn’t exist as a row in the wp_postmeta table.
It makes sense what he says and I’ve checked that the field actually exists on all posts, but some just have NULL values. What’s strange is that some with NULL appears, others don’t. I don’t have acces to the phpmyadmin to check exactly what the DB as.
In the query loop I’ve added a meta query with a compare of EXISTS and NOT EXISTS but the results are the same:
What I Need
Is there a way to configure the Bricks Query Loop to include posts with NULL values in this field when sorting? Ideally, I’d like posts with NULL values to appear at the end of the results (when sorting DESC) or at the beginning (when sorting ASC).
Has anyone else encountered this issue with custom fields and NULL or non existing values?
Thanks in advance for any help or suggestions!