Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Get posts by meta value with date

Depends what you are getting back from the ‘date_event’ post-meta field.

That’s probably the culprit here — if it’s an actual date with a month and day, it’s not going to match what you are getting from date(‘Y’), which is just going to be ‘2018’.

You can check the value in ‘date_event’ on any of the agenda posts with:

print_r( get_post_meta( $post_id, ‘date_event’, true ) );

If the value in ‘date_event’ is a Unix timestamp, that actually makes it easy because then you can just query for all dates starting with Jan 1 of the current year, something like:

$first_of_year = strtotime( "1 January " . date( 'Y' ) );

$args = array(  'post_type' => 'agenda',
    'posts_per_page' => -1,
    'meta_key' => 'date_event',
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'meta_query' => array(
        array(
            'key' => 'date_event',
            'value' => $first_of_year,
            'compare' => '>=',
        ).
    ),
    'tax_query' => array(
        array(
           'taxonomy' => 'event_type',
           'field' => 'term_id',
           'terms' => $type_name,
        )
    )
);

If date_event is giving you a value like 20180320, you could do something similar, but with …

$first_of_year = date( 'Y' ) . '0101';

If you are getting something like ‘2018-03-29’ that’s when you might need to use the ‘type’ => ‘DATE’ — but if you have one of the first two situations, you don’t need it.

One other thing: ‘sort_order’ isn’t a valid param, though it’s just going to be ignored). You just want ‘order’, which you had lower down anyway.

Ans when you are comparing numeric values it’s best practice to change ‘orderby’ => ‘meta_value’ to ‘orderby’ => ‘meta_value_num’.

Related Posts:

  1. Is it possible to retrieve a post and its metadata at the same time?
  2. Look up all posts by status or meta value – performance difference
  3. How to remove Date from Meta Description on SERP?
  4. Sort Posts By Custom Field Date?
  5. Randomizing Post Links Outside of Loop – No Author or Date
  6. How to save HTML data into SQL post_content column
  7. Update post meta date always store 1970-01-07
  8. How can I prevent the post_modified column in wp_posts from being updated?
  9. How to update the ‘modified_time’ of a post ONLY when content is changed?
  10. How do I retrieve the slug of the current page?
  11. Most efficient way to get posts with postmeta
  12. Get posts by meta value
  13. Explanation of update_post_(meta/term)_cache
  14. How to extract data from a post meta serialized array?
  15. How to save an array with one metakey in postmeta?
  16. WordPress is stripping escape backslashes from JSON strings in post_meta
  17. How can I get the post ID from a WP_Query loop?
  18. Check if Post Title exists, Insert post if doesn’t, Add Incremental # to Meta if does
  19. How to update_post_meta value as array
  20. Adding meta tag without plugin
  21. What’s the point of get_post_meta’s $single param?
  22. What is the different between an attachment in wp_posts and an attachment in wp_postmeta?
  23. How to edit a post meta data in a Gutenberg Block?
  24. meta_query: using BETWEEN with floats and/or casting to DECIMAL
  25. Sanitizing integer input for update_post_meta
  26. post formats – how to switch meta boxes when changing format?
  27. Execute action after post is saved with all related post_meta records (data)
  28. Lack of composite indexes for meta tables
  29. Get a single post by a unique meta value
  30. if get_post_meta is empty do something
  31. How we get_post_meta without post id
  32. How get post id from meta value
  33. What is the code to get the download link for a product in WooCommerce?
  34. Safe to delete blank postmeta?
  35. advanced custom fields update_field for field type: Taxonomy
  36. update_post_meta not saving when value is zero
  37. Get Recent Posts by Date in Multisite
  38. Content hooks vs User hooks
  39. Meta compare with date (stored as string) not working
  40. Trying to get custom post meta through Jetpack JSON API [closed]
  41. How to update/insert custom field(post meta) data with wordpress REST API?
  42. Restrict post edit/delete based on user ID and custom field
  43. get_post_meta returning empty string when data shows in the database
  44. publish_post action hook doesn’t give post_meta_data
  45. Remove WordPress.org Meta link
  46. Remove post meta keys
  47. How to access the post meta of a post that has just been published?
  48. Why time functions show invalid time zone when using ‘c’ time format?
  49. Why is get_post_meta returning an array when I specify it as single?
  50. How to update/delete array in post meta value?
  51. How to get all term meta for a taxonomy – getting term_meta for taxonomy
  52. Adding an assisting editor box to Post page
  53. delete unused postmeta
  54. Should I sanitize custom post meta if it is going to be escaped later?
  55. Add post meta based on another post meta value before publish post
  56. How do I retrieve multi-dimensional arrays from the wp_postmeta table, & display on a website?
  57. Permalinks using event date (year & month) instead of publication date
  58. Front-end update_post_meta snippet displays white screen?
  59. Query between two meta values?
  60. Save both current and new version of post meta
  61. Get Advanced Custom Fields values before saving [closed]
  62. Give extra post-meta to RSS feeds
  63. How to get meta value in wp_attachment_metadata
  64. WP REST API “rest_no_route” when trying to update meta
  65. Clean up output added via wp_head()
  66. List posts under meta_value heading
  67. Why am I getting an infinite loop with have_posts?
  68. get_post_meta – get a single value
  69. delete value 0 in post meta [closed]
  70. Can I safely delete a record, manually, in the wp postmeta table?
  71. How to store post meta in an array?
  72. What action hook updates post meta?
  73. Can’t translate the post meta data (Date) in another language
  74. get_post_meta / update_post_meta array
  75. Ordering posts by anniversary using only day and month
  76. adding a URL to a post meta
  77. WP Query post meta value
  78. Exclude a category from the filed under list
  79. Short of raw SQL, can I query for multiple attachment metadata that have a given array key?
  80. Human Time Diff, change mins to minutes
  81. How do I access post meta data when publishing a new post in Gutenberg?
  82. update_post_meta() not working when used with WordPress action
  83. Using Advanced Custom Field (ACF) to insert meta description on each page
  84. Triple meta_key on custom SELECT query
  85. get_post_custom()
  86. Adding meta data to an attachment post
  87. update_post_meta not adding anything.(Nor add_post_meta)
  88. loop through all meta keys with get_post_meta
  89. How to add meta tag to wordpress posts filter?
  90. Order posts according to user defined order for meta values?
  91. Displaying posts with only upcoming dates according their custom field date value
  92. Are multiple values from get_post_meta guaranteed to be ordered?
  93. Identifying Importer Posts
  94. Get updated post meta on save_post action?
  95. Get post from meta_key and meta_value
  96. Add a post metadata if only the key and value does not exist
  97. get_post_meta returns bool(false)
  98. How metadata API works?
  99. Correct processing of `$_POST`, following WordPress Coding Standards
  100. Metabox Data not being saved [closed]
Categories post-meta Tags date, get-posts, post-meta
How to embed a WordPress article on another website [closed]
WordPress is executing URL in code when called via wp_mail()

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress