For this to work reliably, the date format in the database should be yyyy-mm-dd
. Comparison type should be DATE
or NUMERIC
. If you take the date of 25-04-2012 in present format and compare it numerically to the date 26-04-1986, you can see what the issue will be: 25042012 < 26041986
ALso- if you’re doing these queries in addition to your main loop, use a new instance of WP_Query.
EDIT-
there were a few errors you didn’t fix. note that this still won’t work quite right until you fix the date format.
<?php
$today = date('m/d/Y', strtotime('+2 hours'));
$the_query = new WP_Query( array(
'post_type' => 'events',
'posts_per_page' => 5,
'meta_key' => 'start_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'start_date',
'value' => $today,
'compare' => '>=',
'type' => 'DATE'
))
));
?>
Related Posts:
- Ordering posts by anniversary using only day and month
- Ordering posts by custom field named “date” in backend
- How can I query on the year part of a complete date in a custom field?
- Which is best in the following scenario : post_meta vs custom table vs parent/child posts
- How to add a new meta key and assign timestamp to posts
- How do I have WP_Query match posts based on search parameter OR meta fields? (rather than search parameters AND meta fields)?
- Unique meta_key with array value vs repeated meta_key with single values
- Avoiding ACF get_field and returning to core WordPress function
- Query post by date (stored custom field meta as yyyymmdd) and differentiate across 12 months
- change order of images attached to post
- Group Posts By Custom Meta Value: Date
- How to improve my non-unique metadata MySQL entries?
- Checking if field is set before comparing with meta_query in query_posts?
- WP Query Args – search by meta_key or title
- SQL query based on two different custom field values
- Using meta query (‘meta_query’) with a search query (‘s’)
- Can I exclude a post by meta key using pre_get_posts function?
- Custom post meta field effect on the performance on the post
- How to get custom post meta using REST API
- Difference between meta keys with _ and without _ [duplicate]
- Orderby meta_value only returns posts that have existing meta_key
- What is the index [0] for on post meta fields?
- What is “meta_input” parameter in wp_insert_post() used for?
- How to enable revisions for post meta data?
- The “_encloseme” Meta-Key Conundrum
- ORDER BY custom field value
- Best way to programmatically remove a category/term from a post
- Using get_post_meta with new_to_publish
- Can I query custom meta data through WP_Query
- 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
- Custom field metabox not showing in back-end
- Meta Query with AND & OR?
- So much data in postmeta
- Can I count the number of users matching a value in a multiple value key?
- Getting attachments by meta value
- When using add_post_meta and update_post_meta, is there any way to give the individual arrays keys?
- How to hide meta box values from custom fields list?
- Delete custom meta
- Query Posts based on custom field value
- Problem saving meta data
- Check if value exists before saving
- Echo out custom fields in comments
- Get meta value when the page is a blog archive
- Search one custom field?
- Can’t save meta field value if the title not set
- Create custom query for search?
- Display Data From This Custom Media Upload Meta Box?
- Save values generated via API as custom meta fields
- Query postmeta values, and return multiple post_titles for common meta value
- Filter query based on date in custom field
- When post is updated, custom metadata in text area field is overwritten
- Thirtieth century date for a post
- change attachment custom field onChange event
- Adding custom fields to images
- How to save dynamically generated value in post type?
- Order a query by meta_value and then by post_date
- wordpress simple post multi rating with post_meta and user_meta
- Media Attachment Custom Meta Fields not saving in Media Uploader when using jQuery UI Autocomplete
- Query Posts Via WordPress URL
- wp_postmeta are updated for only one page
- update_post_meta not working in action hook
- how to show this part only in the single post page?
- Custom Field: Display only if a specific key is selected outside the loop
- WordPress archives by custom field and date
- Meta_query not filtering posts
- Complex WP_Query Using Post Date And Post Meta
- Display meta data from a custom field within plugin Category Grid View Gallery
- When editing a post with a custom meta box the values aren’t displaying correctly
- Display posts where date field matches current month?
- Hard Define Custom Field Value
- Querying meta values within an array
- Custom fields / meta box output
- If post custom meta data is not set, show normal state
- add_post_meta insert null value into Database
- Make Custom Fields Public in JSON – API
- How do i output images from URL’s added to the same custom field key
- Retrieving custom field as shortcode
- Colecting values from custom field checkboxes and displaying them in the post
- Build Array from Input Fields question
- How do delete a meta key?
- Filter Query Post by Custom Fields(by date)
- custom field with links
- add multiple values (array) to post meta_input
- Order by a meta field in query loop
- Dynamic background image used in css after selector
- How can i put a custom field inside this php
- Check for custom field value in different post type than current one and do something
- Dynamically update Custom Fields to display new dates
- Site uses wpdb to fetch meta_keys but just displays first meta_key from a page (the post uses the same meta_key “filmmaker” more than once)
- save all acf options in one meta_value [closed]
- How to add new Metadata options (Date, Author, etc.) for Posts?
- Saving multiple custom meta box fields
- Search for meta_query does not return any result if combined with title
- get Custom field label (select/dropdown) on front end
- Best Practice for storing aggregate data by date or other criteria?
- Block Editor – Meta values not saved, meta changes to empty array on update
- Sort posts based on an acf field called fecha value return longtext ‘20240517’
- What is the best way to get a different post’s custom field/postmeta with js?