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?
- WP_Query – Order results by meta value
- Using meta query (‘meta_query’) with a search query (‘s’)
- Query to sort a list by meta key first (if it exists), and show remaining posts without meta key ordered by title
- Meta query with string starting like pattern
- compare meta_query in get_posts arguments
- ORDER BY custom field value
- 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
- Filtering a WP_Query meta_query by numeric values isn’t working
- Getting attachments by meta value
- How do I search an array stored in a custom-field using WP_Query?
- querying with custom meta field with meta_query
- How do I order by multiple custom fields using wp_query?
- Order by meta_value_num DESC and meta_value ASC on WP 4.0
- Query meta field using between
- Order Posts by Closest Numeric Values
- Is there a way to do multiple ordering on a multiple meta_query?
- 2 orderby in wp_query with 2 custom fields
- Can serialized arrays in DB be matched against serialized arrays with meta_query?
- How to get a meta value from all post
- WordPress Search Custom Meta Field Only
- WordPress altering my custom query, How to fix it?
- Ordering posts by anniversary using only day and month
- Calling Specific Pages with wp query Part II
- 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
- Querying by taxonomy vs Querying by Custom fields Speed Comparison
- Slow meta query with multi meta keys
- 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?
- WP_query : meta_key with custom rule for specific value
- Displaying posts with only upcoming dates according their custom field date value
- How to filter a dd/mm/yyyy date from a custom field in a query
- meta_key and meta_value not working together
- Using WP Query to search by multiple meta fields
- Sorting posts by multiple values, combined
- Compare WP Custom Field date
- meta_value_num sort glitch
- Sort by posts that have a featured image?
- Order by value in serialized custom field
- Which is best in the following scenario : post_meta vs custom table vs parent/child posts
- WP_Meta_Query causing long-running MySQL queries
- $wp_query meta_key naming issue [closed]
- Author Page Custom Query WHERE author OR [post meta value] OR [post meta value]
- Meta_query and numeric comparison [closed]
- Order Posts by meta value AND published date
- 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
- Order by custom field value not working for acf date field
- Adding Custom Fields to Search
- Upcoming Event: How do I sort database by custom date field, but ignore past dates?
- Any way to include custom fields in WP_Query results?
- Complex WP_User_Query call fails on production server
- Multiple relationships in a query
- WP Query Returning All Posts
- How do I use wp_query for WordPress search?
- Orderby custom field meta value ASC and then by date DESC
- Conditional custom field query
- Order query by meta_value with multiple custom fields
- WP_Query: include custom post type only with specific meta value
- Returning a list of custom post types excluding those without a specific meta_value
- How to make Meta Query case sensitive?
- 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
- How make a custom search on backend in WordPress without plugin?
- Auto Populate Custom Field with Complex Value That Increase by One?
- How to add a new meta key and assign timestamp to posts
- Get content from pages with same meta_key from Database
- WP 3.1 meta_query for multiple custom field values
- Fetch Record based on meta key dates
- Meta_query with multiple keys and multiple values
- meta_query BETWEEN, but the range is stored in the custom field
- Extend product search with meta in WooCommerce
- How do I have WP_Query match posts based on search parameter OR meta fields? (rather than search parameters AND meta fields)?
- how to make members list directory through wordpress post custom meta key.
- Large AND OR query timing out
- Get posts with same meta value as current post
- Alter main archive, to show posts with meta as last
- Meta Query returns wrong number of posts
- meta_value_num not ordering all items
- Possible to filter custom post type with multiple meta data?
- Custom WP_Query for WordPress Search Results with meta_query