To sort based on a date field, you need to convert the date to a UNIX datetime format. It’s probably best to store the date in a separate custom field. I’ve never had to set this up, but you’d
- hook into save_post
- look for a custom field with the date in it
- convert that to a datetime in a different custom field (probably started with an underscore to hide it from the custom fields interface).
- Query for the hidden custom field and use it to sort.
There are also some custom meta box plugins that come with a date picker and the ability to store UNIX format dates.
To do the human-readable date to UNIX conversion, you’ll want to learn about PHP’s strtotime()
function. To convert back (if you’re storing the UNIX date and want to print it in a human-readable format), you’ll use the date()
function.
Related Posts:
- Can I query custom meta data through WP_Query
- Meta Query with AND & OR?
- Trying to perform complex custom field query with order by set to field value
- Compare meta_query decimals not working right
- Using OR in WP_Query negates the “NOT EXISTS” compare
- How can I combine meta_query queries?
- Difference between ‘LIKE’ and ‘IN’ in meta queries
- Meta query with timestamp using WP_query
- Meta Query And/Or
- Query post by date (stored custom field meta as yyyymmdd) and differentiate across 12 months
- How can I modify my meta_query to work with prices that are stored in the database that contain dollar signs and commas?
- Query to sort a list by meta key first (if it exists), and show remaining posts without meta key ordered by title
- compare meta_query in get_posts arguments
- Filter WP_Query for posts having a certain meta-value
- Add custom fields to search
- SELECT max(meta_value) FROM wp_postmeta WHERE meta_key=’price’… stops working when value is over 999
- Matching Serialized Arrays with meta_query
- WP_Query orderby custom field then post_date in one query
- Getting attachments by meta value
- How do I search an array stored in a custom-field using WP_Query?
- Order by meta_value_num DESC and meta_value ASC on WP 4.0
- 2 orderby in wp_query with 2 custom fields
- How to get a meta value from all post
- WordPress Search Custom Meta Field Only
- WordPress altering my custom query, How to fix it?
- WP_Query multiple use of relation and/or
- Loop through two different sets of custom fields
- Query Problem – Show posts within category ‘x’ that have a custom field between ‘y’ and ‘z’
- Comparing timestamps in meta query doesn’t work
- Display two post types ordered by two custom fields
- Order by meta_key in custom post type doesn’t affect the query
- How can I query on the year part of a complete date in a custom field?
- meta_key and meta_value not working together
- meta_value_num sort glitch
- Which is best in the following scenario : post_meta vs custom table vs parent/child posts
- Sorting posts by custom fields in meta_query
- Filtering by Post Meta Custom Fields – Performance
- meta_query for a string inside a meta field containing a comma-separated list
- Any way to include custom fields in WP_Query results?
- Complex WP_User_Query call fails on production server
- Orderby custom field meta value ASC and then by date DESC
- Returning a list of custom post types excluding those without a specific meta_value
- Two near-identical custom field types – one works, the other doesn’t . What can cause this?
- wp_query check if integer exists in custom field’s array
- Auto Populate Custom Field with Complex Value That Increase by One?
- WP 3.1 meta_query for multiple custom field values
- Fetch Record based on meta key dates
- meta_query BETWEEN, but the range is stored in the custom field
- Large AND OR query timing out
- How to combine custom fields to make one order-able value
- Use meta_query to get title of associated post
- Meta query with multiple custom fields for archives page ordering problem
- filter custom field values $min $max
- Query based on custom fields start and end date
- Ordering posts by custom field named “date” in backend
- Unique meta_key with array value vs repeated meta_key with single values
- Is it possible to retrieve all posts with a certain value for metadata?
- WP Query – Is this correct?
- comment meta_query for keys that aren’t yet set
- Select custom posts by meta_value and sort by a different meta value
- Adding custom field and querying from post table
- Getting a custom post’s custom field based on another custom post’s custom field select
- Display posts with specific value first in query
- change order of images attached to post
- tax query between operator like
- How to query for posts with either one or another custom field
- Is it possible to compare the current time with a custom “start” and “end date
- How to update custom field of a posts in a particular category
- Query multiple post types, but different order for each
- Order by the first array within a meta_query
- Query ACF relationship field – Comparator IN – Value array
- multiple meta_query and orderby question
- ACF: How to query for a given value count of an array like field? (e.g.: How many rows has a `flexible_content` field?)
- get posts based on non-single metadata
- How to _GET multiple value checkbox WP_Query in Custom Toxonomy / Custom Fields
- Woocommerce products search with custom fields
- How to save multiple values with same meta_key, each value linked to another tag id
- How to improve my non-unique metadata MySQL entries?
- Trouble with serialized metadata
- Comparing Meta Field date in WPQuery using Meta_Query?
- Query against multiple locations within single custom post type post
- WP_Query, ACF field and array
- Query to sort a list by meta key first (if it exists), and show remaining posts without meta key ordered by title
- Order by Date Custom Field
- Query multiple meta values at the same time :
- Create Shortcode shows only posts with custom_field meta
- meta_query ‘compare’ => ‘!=’ not working
- Using custom field as ‘orderby’ value causing post grouping by publish date
- Filter query based on date in custom field
- WP Query multiple select form – meta_query help
- WP_Query based on multiple metadata comparisons
- Checking if field is set before comparing with meta_query in query_posts?
- Display posts where date field matches current month?
- Querying meta values within an array
- How do delete a meta key?
- Filter Query Post by Custom Fields(by date)
- Query posts by Custom Meta (checkbox) & Genesis Grid Loop
- Get posts by meta value except one post [closed]
- Dynamic background image used in css after selector
- WP Query Args – search by meta_key or title