{"id":5031,"date":"2018-04-24T11:17:21","date_gmt":"2018-04-24T15:17:21","guid":{"rendered":"https:\/\/themespress.ca\/?p=5031"},"modified":"2022-10-01T14:32:38","modified_gmt":"2022-10-01T18:32:38","slug":"wordpress-query-display-post-type-results-with-two-date-fields","status":"publish","type":"post","link":"https:\/\/themespress.ca\/en\/wordpress-query-display-post-type-results-with-two-date-fields\/","title":{"rendered":"WordPress Query: Display Post Type Results with Two Date Fields"},"content":{"rendered":"<div class=\"wpb-content-wrapper\"><div class=\"vc_row wpb_row vc_row-fluid\"><div class=\"wpb_column vc_column_container vc_col-sm-12\"><div class=\"vc_column-inner\"><div class=\"wpb_wrapper\">\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>With this tutorial, we want to return Post Type results with two date fields. In order to do so, we first need to create a Post Type. For our example, we already have a <strong>promotions<\/strong> Post Type. Within this Post Type, we&#8217;ve added a <strong>metabox with Two date type fields<\/strong>.<\/p>\n<p>Both <strong>Date type fields<\/strong> will contain the Date field (Metakey) for the event start date and end date. Here are both field IDs:<strong>start-date-promotion<\/strong> and <strong>end-date-promotion<\/strong>.<\/p>\n<p>With our Post Type and both fields with a specific date added, we need to initialize today&#8217;s date before our arguments so we can compare our meta query with the today&#8217;s date.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading\" >Display Post Type Results with Two Date Fields.<\/h2>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>(For our example, we display on-going promotions.)<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-4535943\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-4535943\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"php\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">&lt;?php \/\/ WP_Query arguments\n$today = date(&#039;Y-m-d&#039;);\n$args = array(\n  &#039;post_type&#039;         =&gt; array( &#039;promotions&#039; ),\n  &#039;meta_query&#039;        =&gt; array(\n\tarray(\n\t&#039;key&#039;\t\t=&gt; &#039;date-debut-promotion&#039;,\n\t&#039;compare&#039;\t=&gt; &#039;&lt;=&#039;,\n\t&#039;value&#039;\t\t=&gt; $today,\n        ),\n        array(\n\t&#039;key&#039;\t\t=&gt; &#039;date-fin-promotion&#039;,\n\t&#039;compare&#039;\t=&gt; &#039;&gt;=&#039;,\n\t&#039;value&#039;\t\t=&gt; $today,\n\t)\n   ),\n);\n \n\/\/ The Query\n$query = new WP_Query( $args );\n\/\/ The Loop\nif ( $query-&gt;have_posts() ) {\nwhile ( $query-&gt;have_posts() ) { \n$query-&gt;the_post(); \n?&gt;\n \n\/\/ Afficher votre loop ici\n \n&lt;?php } } else { ?&gt;\n&lt;?php } ?&gt;\t\n&lt;?php wp_reset_postdata(); ?&gt;<\/pre><\/div>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>So we have created a variable that stores today&#8217;s day and then we create our query and finally we assign a meta query or a date field <strong>start-date-promotion<\/strong> that needs to be lower or equal to today&#8217;s date and that the <strong>end-date-promotion<\/strong> is greater or equal than today&#8217;s date.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"With this tutorial, we want to return Post Type results with two date fields. In order to do so, we first need to create a Post Type. For our example,<div class=\"read-more\"><a class=\"btn button-secondary\" href=\"https:\/\/themespress.ca\/en\/wordpress-query-display-post-type-results-with-two-date-fields\/\">Read More<\/a><\/div>","protected":false},"author":81,"featured_media":5034,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[283],"tags":[],"class_list":["post-5031","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/5031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/users\/81"}],"replies":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/comments?post=5031"}],"version-history":[{"count":0,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/5031\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media\/5034"}],"wp:attachment":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media?parent=5031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/categories?post=5031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/tags?post=5031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}