Sort by Custom Date

Forums › Forums › Search & Filter Pro › Sort by Custom Date

Viewing 9 posts – 1 through 9 (of 9 total)
  • Anonymous
    #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
    #9838

    Hey 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 the posts 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
    #9875

    Hey 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/02

    etc.

    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?

    Anonymous
    #9936

    Hey Ross,

    Sorry to bump this up, but do you have any thoughts on this?

    Ross Moderator
    #9939

    Hey 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
    #9944

    Ross,

    Thank you. This helps a lot and I think I might be able to solve it like this 🙂

    Anonymous
    #10126

    Hi 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
    #10129

    Have you tried those yet?

    The beauty of storing dates as timestamps, or in YYYYMMDD format – is because it is numerical sorting!

    🙂

    Anonymous
    #10138

    Fantastic, didn’t realize! 😀

Viewing 9 posts – 1 through 9 (of 9 total)
Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.