{"id":18500,"date":"2013-10-31T15:01:36","date_gmt":"2013-10-31T07:01:36","guid":{"rendered":"https:\/\/www.hongkiat.com\/blog\/?p=18500"},"modified":"2025-04-04T01:38:15","modified_gmt":"2025-04-03T17:38:15","slug":"wordpress-date-query","status":"publish","type":"post","link":"https:\/\/www.hongkiat.com\/blog\/wordpress-date-query\/","title":{"rendered":"Exploring WordPress Date Queries for Post Filtering"},"content":{"rendered":"<p>We can display posts published on specific dates with the date parameters in WP_Query. The functionality, however, is basic. We can query posts by day, year, and month, but we were not able to query posts within a certain timeframe. Having been a victim once, I used a <a href=\"https:\/\/wordpress.org\/\/support\/topic\/will-this-code-limit-the-posts-displayed-to-the-last-14-days\" rel=\"nofollow noopener\" target=\"_blank\">hack<\/a>.<\/p>\n<p>However, thanks to Alex Millis who contributed the code, in version 3.7, released recently, WordPress included <strong>Advanced Date Query<\/strong> which allows us to display more complex date-based post queries natively with <code>WP_Query<\/code>. This enhancement enables displaying posts published before or after specific hours. Let\u2019s dive deeper into this feature.<\/p>\n<h2>Utilizing Advanced Date Queries<\/h2>\n<p>With version 3.7, a new parameter called <code>date_query<\/code> was introduced. How do we utilize this new feature?<\/p>\n<p>Imagine you\u2019re managing a news site and aim to feature articles from the previous week. By leveraging <code>date_query<\/code>, you can easily achieve this as shown below:<\/p>\n<pre>\r\n$last_week_news = new WP_Query( array(\r\n    'date_query' =&gt; array(\r\n        array(\r\n            'after' =&gt; '1 week ago',\r\n        ),\r\n    ),\r\n    'posts_per_page' =&gt; 5,\r\n));\r\n$query = new WP_Query( $last_week_news );\r\n<\/pre>\n<p><strong>Featuring Posts Within Specific Timeframes<\/strong>. Below is how you can showcase posts from 15 December 2012 to 15 January 2013, perfect for highlighting end-of-year and New Year narratives.<\/p>\n<pre>\r\n$new_year_stories = new WP_Query( array(\r\n    'date_query' =&gt; array(\r\n        array(\r\n            'after' =&gt; 'December 15th, 2012',\r\n            'before' =&gt; 'January 15th, 2013',\r\n        ),\r\n    ),\r\n    'posts_per_page' =&gt; 5,\r\n));\r\n$query = new WP_Query( $new_year_stories );\r\n<\/pre>\n<p>This feature is not limited to dates; it also allows for the display of posts published at specific hours.<\/p>\n<p>For instance, to highlight morning news on your site, you can configure <code>date_query<\/code> as follows:<\/p>\n<pre>\r\n$morning_news = array(\r\n    'date_query' =&gt; array(\r\n        array(\r\n            'hour' =&gt; 6,\r\n            'compare' =&gt; '&gt;=',\r\n        ),\r\n        array(\r\n            'hour' =&gt; 9,\r\n            'compare' =&gt; '&lt;=',\r\n        ),\r\n    ),\r\n    'posts_per_page' =&gt; 10,\r\n);\r\n$query = new WP_Query( $morning_news );\r\n<\/pre>\n<p>This approach is simple, direct, and enhances code readability.<\/p>\n<h2>Concluding Remarks<\/h2>\n<p>With the introduction of advanced date query capabilities, <code>WP_Query<\/code> has expanded its utility. This feature is invaluable for sites where date tracking is crucial, such as for event organizers or conference hosts. It offers a powerful tool for managing content visibility based on time parameters.<\/p>\n<p>For additional information, please visit the WordPress Codex page on <a href=\"https:\/\/codex.wordpress.org\/Class_Reference\/WP_Query#Date_Parameters\" rel=\"nofollow noopener\" target=\"_blank\">Date Parameters<\/a>.<\/p>\n<p><strong>Note:<\/strong> This post was first published on the Oct 31, 2013.<\/p>","protected":false},"excerpt":{"rendered":"<p>We can display posts published on specific dates with the date parameters in WP_Query. The functionality, however, is basic. We can query posts by day, year, and month, but we were not able to query posts within a certain timeframe. Having been a victim once, I used a hack. However, thanks to Alex Millis who&hellip;<\/p>\n","protected":false},"author":113,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[49],"tags":[4663,252],"topic":[4520],"class_list":["entry-content","is-maxi"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.8 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>WordPress Date Query<\/title>\n<meta name=\"description\" content=\"We can display posts published on specific dates with the date parameters in WP_Query. The functionality, however, is basic. We can query posts by day,\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.hongkiat.com\/blog\/wordpress-date-query\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Exploring WordPress Date Queries for Post Filtering\" \/>\n<meta property=\"og:description\" content=\"We can display posts published on specific dates with the date parameters in WP_Query. The functionality, however, is basic. We can query posts by day,\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hongkiat.com\/blog\/wordpress-date-query\/\" \/>\n<meta property=\"og:site_name\" content=\"Hongkiat\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/hongkiatcom\" \/>\n<meta property=\"article:published_time\" content=\"2013-10-31T07:01:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-03T17:38:15+00:00\" \/>\n<meta name=\"author\" content=\"Thoriq Firdaus\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@tfirdaus\" \/>\n<meta name=\"twitter:site\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Thoriq Firdaus\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wordpress-date-query\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wordpress-date-query\\\/\"},\"author\":{\"name\":\"Thoriq Firdaus\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e7948c7a175d211496331e4b6ce55807\"},\"headline\":\"Exploring WordPress Date Queries for Post Filtering\",\"datePublished\":\"2013-10-31T07:01:36+00:00\",\"dateModified\":\"2025-04-03T17:38:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wordpress-date-query\\\/\"},\"wordCount\":281,\"commentCount\":7,\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"keywords\":[\"ad-divi\",\"WordPress Tips\"],\"articleSection\":[\"WordPress\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wordpress-date-query\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wordpress-date-query\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wordpress-date-query\\\/\",\"name\":\"WordPress Date Query\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\"},\"datePublished\":\"2013-10-31T07:01:36+00:00\",\"dateModified\":\"2025-04-03T17:38:15+00:00\",\"description\":\"We can display posts published on specific dates with the date parameters in WP_Query. The functionality, however, is basic. We can query posts by day,\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wordpress-date-query\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wordpress-date-query\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wordpress-date-query\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Exploring WordPress Date Queries for Post Filtering\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"name\":\"Hongkiat\",\"description\":\"Tech and Design Tips\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\",\"name\":\"Hongkiat.com\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"contentUrl\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"width\":1200,\"height\":799,\"caption\":\"Hongkiat.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/hongkiatcom\",\"https:\\\/\\\/x.com\\\/hongkiat\",\"https:\\\/\\\/www.pinterest.com\\\/hongkiat\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e7948c7a175d211496331e4b6ce55807\",\"name\":\"Thoriq Firdaus\",\"description\":\"Thoriq is a writer for Hongkiat.com with a passion for web design and development. He is the author of Responsive Web Design by Examples, where he covered his best approaches in developing responsive websites quickly with a framework.\",\"sameAs\":[\"https:\\\/\\\/thoriq.com\",\"https:\\\/\\\/x.com\\\/tfirdaus\"],\"jobTitle\":\"Web Developer\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/author\\\/thoriq\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"WordPress Date Query","description":"We can display posts published on specific dates with the date parameters in WP_Query. The functionality, however, is basic. We can query posts by day,","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.hongkiat.com\/blog\/wordpress-date-query\/","og_locale":"en_US","og_type":"article","og_title":"Exploring WordPress Date Queries for Post Filtering","og_description":"We can display posts published on specific dates with the date parameters in WP_Query. The functionality, however, is basic. We can query posts by day,","og_url":"https:\/\/www.hongkiat.com\/blog\/wordpress-date-query\/","og_site_name":"Hongkiat","article_publisher":"https:\/\/www.facebook.com\/hongkiatcom","article_published_time":"2013-10-31T07:01:36+00:00","article_modified_time":"2025-04-03T17:38:15+00:00","author":"Thoriq Firdaus","twitter_card":"summary_large_image","twitter_creator":"@tfirdaus","twitter_site":"@hongkiat","twitter_misc":{"Written by":"Thoriq Firdaus","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.hongkiat.com\/blog\/wordpress-date-query\/#article","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/wordpress-date-query\/"},"author":{"name":"Thoriq Firdaus","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e7948c7a175d211496331e4b6ce55807"},"headline":"Exploring WordPress Date Queries for Post Filtering","datePublished":"2013-10-31T07:01:36+00:00","dateModified":"2025-04-03T17:38:15+00:00","mainEntityOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/wordpress-date-query\/"},"wordCount":281,"commentCount":7,"publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"keywords":["ad-divi","WordPress Tips"],"articleSection":["WordPress"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.hongkiat.com\/blog\/wordpress-date-query\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.hongkiat.com\/blog\/wordpress-date-query\/","url":"https:\/\/www.hongkiat.com\/blog\/wordpress-date-query\/","name":"WordPress Date Query","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/#website"},"datePublished":"2013-10-31T07:01:36+00:00","dateModified":"2025-04-03T17:38:15+00:00","description":"We can display posts published on specific dates with the date parameters in WP_Query. The functionality, however, is basic. We can query posts by day,","breadcrumb":{"@id":"https:\/\/www.hongkiat.com\/blog\/wordpress-date-query\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hongkiat.com\/blog\/wordpress-date-query\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.hongkiat.com\/blog\/wordpress-date-query\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.hongkiat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Exploring WordPress Date Queries for Post Filtering"}]},{"@type":"WebSite","@id":"https:\/\/www.hongkiat.com\/blog\/#website","url":"https:\/\/www.hongkiat.com\/blog\/","name":"Hongkiat","description":"Tech and Design Tips","publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.hongkiat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.hongkiat.com\/blog\/#organization","name":"Hongkiat.com","url":"https:\/\/www.hongkiat.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","contentUrl":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","width":1200,"height":799,"caption":"Hongkiat.com"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/hongkiatcom","https:\/\/x.com\/hongkiat","https:\/\/www.pinterest.com\/hongkiat\/"]},{"@type":"Person","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e7948c7a175d211496331e4b6ce55807","name":"Thoriq Firdaus","description":"Thoriq is a writer for Hongkiat.com with a passion for web design and development. He is the author of Responsive Web Design by Examples, where he covered his best approaches in developing responsive websites quickly with a framework.","sameAs":["https:\/\/thoriq.com","https:\/\/x.com\/tfirdaus"],"jobTitle":"Web Developer","url":"https:\/\/www.hongkiat.com\/blog\/author\/thoriq\/"}]}},"jetpack_featured_media_url":"https:\/\/","jetpack_shortlink":"https:\/\/wp.me\/p4uxU-4Oo","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/18500","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/users\/113"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/comments?post=18500"}],"version-history":[{"count":3,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/18500\/revisions"}],"predecessor-version":[{"id":73633,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/18500\/revisions\/73633"}],"wp:attachment":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/media?parent=18500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/categories?post=18500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/tags?post=18500"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/topic?post=18500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}