Forums › Forums › Search & Filter Pro › Sort by Custom Date
Tagged: acf, custom field, query
- This topic has 8 replies, 2 voices, and was last updated 9 years, 10 months ago by Anonymous.
-
Anonymous(Private) January 6, 2015 at 12:55 pm #9820
I have an issue I’m not sure how to deal with regarding how a post type is initially sorted. All of the posts have a custom ACF text field with a number that is formatted like a date: 2000/05 (Full year/mm). The problem is that it is not a date field, merely a text field and I’m not sure how to change the sort_by query to correctly sort the posts (newest to oldest) using this field.
I’m using a custom template coded like this:
if ( $query->have_posts() ) { while ($query->have_posts()) { $query->the_post(); // the article } }
How can I correctly change the query to sort the posts through my ACF custom field?
Ross Moderator(Private) January 6, 2015 at 5:55 pm #9838Hey Stian
Sorting by date requires a specific save format as mentioned on the plugin homepage.
The best way to save the date in the database in ACF is YYYYMMDD – this means that if you sort the date numerically you will also be sorting chronologically – this is the best way for saving a date in any database in general (or as a timestamp) as numeric operations are fast and easy to perform.
You could try the following but I cannot guarantee it will work without updating the way you store your dates:
In S&F
defaults & settings
box, click theposts
tab.In here you have your default sort order, in the first option choose
Meta Value
– then some more options will appear.Choose your meta key, and try sorting by
numeric
– I’m hoping this will do what you require – but I cannot guarantee it without making some modifications to the way you store your dates.Thanks
Anonymous(Private) January 7, 2015 at 9:57 am #9875Hey Ross, thanks for the quick answer.
I’m afraid this didn’t work, however it almost did. The posts now get sorted like this:
2014/01
2014/07
2014/03
2014/05
2014/02
2013/03
2013/06
2013/02etc.
It sorts the first number, but completely ignores the second part (which makes sense as it’s not saving as a date).
I was hoping there was a way for me to convert the field in the query, as converting this text field to a date field would be too much work (hundreds of posts), unless you could recommend a more automated way to do this?
Ross Moderator(Private) January 8, 2015 at 2:49 pm #9939Hey Stian
I was having a think but unfortunately not.
When you use this format
2013/02
it is very had for most software to be able to treat it properly as a date for comparisons and sorting.There are two (maybe three) things you can do as far as I can see:
1) Manually change them all…
2) Create a custom script, which loops through each post, pulls the meta data, reformats it, and updates the post
3) Use a plugin – there may or may not be one out there for your needs – what you need is quite specific… but this is a google I just did which may yield some results https://www.google.co.uk/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=bulk%20edit%20post%20meta%20fields and I also found this – https://wordpress.org/plugins/custom-bulkquick-edit/Hope that helps!!
Anonymous(Private) January 12, 2015 at 8:58 am #10126Hi Ross,
I didn’t notice before, but there doesn’t even seem to be a way to sort by custom date..
I managed to convert all the fields to correctly saved date fields, but sorting by meta value only gives two options: numeric and alphabetic.
How can I actually sort this as a custom date?
Ross Moderator(Private) January 12, 2015 at 1:52 pm #10129Have you tried those yet?
The beauty of storing dates as timestamps, or in YYYYMMDD format – is because it is numerical sorting!
๐
-
AuthorPosts