Skip Navigation

[Resolved] Null values don’t show in View sorted by custom field

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a View that shows posts and custom field values from those posts. The View is sorted by the custom field. If one of the results in the View does not contain a value for that custom field, it is not included in the search results.

Solution: This is the result of a quirk in WordPress queries. When sorting by a custom field, posts that do not contain a value for that custom field are not included in the search results. There are two workarounds:

1. Give every post a death date value. Choose the same date like January 1, 1700, for each post that does not have a death date. Use conditional HTML to hide January 1, 1700 on the front-end of the site and display something different like "Unknown". You can accomplish this with no extra custom code.

2. Or, create another custom field that holds a number. Copy the timestamp from the death date custom field into the number field when a post is saved or edited. You need custom code for this. Use the save_post hook, or cred_save_data if these posts are only added and edited by CRED. If no value is provided for the death date in a post, store a "1" in the new custom field. Sort by this number custom field, but display the death date field.

This support ticket is created 6 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by Griffin 6 years, 6 months ago.

Assisted by: Christian Cox.

Author
Posts
#644565

I am trying to: show all custom posts but if date of death is null, it will not show

Link to a page where the issue can be seen: hidden link

I expected to see: all values returned

Instead, I got: only values with dates

#645039

Hi, is the problem View sorted by the death date field? There is a known quirk in WordPress queries - if you try to sort a query by a custom field (meta) value, posts that do not include a value for that custom field will not be included in the results.

#649827

That is the problem. Is there any solution? It's probably a mySQL issue, but it would be nice if there was a way around it.

#649898

There are a couple of workarounds I can think of.
1. Give every post a death date value. Choose the same date like January 1, 1700, for each post that does not have a death date. Use conditional HTML to hide January 1, 1700 on the front-end of the site and display something different like "Unknown". You can accomplish this with no extra custom code.
2. Create another custom field that holds a number. Copy the timestamp from the death date custom field into the number field when a post is saved or edited. You need custom code for this. Use the save_post hook, or cred_save_data if these posts are only added and edited by CRED. If no value is provided for the death date in a post, store a "1" in the new custom field. Sort by this number custom field, but display the death date field.

#667786

Thanks