{"id":5041,"date":"2018-04-22T14:19:34","date_gmt":"2018-04-22T18:19:34","guid":{"rendered":"https:\/\/themespress.ca\/?p=5041"},"modified":"2022-10-01T14:34:42","modified_gmt":"2022-10-01T18:34:42","slug":"wordpress-display-posts-of-the-month-with-get_posts","status":"publish","type":"post","link":"https:\/\/themespress.ca\/en\/wordpress-display-posts-of-the-month-with-get_posts\/","title":{"rendered":"WordPress: Display Posts of the Month with get_posts"},"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&#8217;ll show you how to use a query with the database so that it displays posts or Post Types that were published during any given month.<\/p>\n<p>The <a href=\"https:\/\/codex.wordpress.org\/Template_Tags\/get_posts\" target=\"_blank\" rel=\"noopener\">get_posts<\/a> is a great tool to search within a database but it can restrict the usable parameters. It can simplifies queries in any page template.<\/p>\n<p>It also allows us to display a number of results via the post_per_page without pagination. We will need to make sure to close our query with<br \/>\n<strong>wp_reset_postdata();<\/strong>.<\/p>\n<p>In our example below, we&#8217;ve configured <strong>posts_per_page<\/strong> to -1 because we want to display the current month only. Our Post Type is <strong>viavideos<\/strong>, so either replace it by your or <strong>\u2018post\u2019<\/strong> for articles.<\/p>\n<p>As you can see, we need to stock two different variables in the dates which represent the current month and year. Then with our parameters we need to do a <a href=\"https:\/\/codex.wordpress.org\/Class_Reference\/WP_Query\" target=\"_blank\" rel=\"noopener\">date_query<\/a> and add our two date parameters.<\/p>\n<p>And then we close the loop to display our results.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading vc_custom_1664649280627\" >Display Post or Post Type of the month with the get_posts function.<\/h2><div id=\"ts-enlighterjs-container-6906364\" 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-6906364\" 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\n$year = date(&#039;Y&#039;); \n$month = date(&#039;m&#039;); \n$args = array( \n\t&#039;posts_per_page&#039;    =&gt; -1,\n\t&#039;post_type&#039;         =&gt; &#039;viavideos&#039;,\n\t&#039;date_query&#039;        =&gt; array(\n\tarray(\n\t\t&#039;year&#039;  =&gt; $year,\n\t\t&#039;month&#039; =&gt; $month,\n\t),\n),\n);\n \n$posts = get_posts( $args );\nforeach( $posts as $post ) :  setup_postdata($post); ?&gt;\n&lt;?php \/\/\/ Votre div dynamique d&eacute;sir&eacute;e ?&gt;\n&lt;?php endforeach; wp_reset_postdata(); ?&gt;<\/pre><\/div><\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"With this tutorial, we'll show you how to use a query with the database so that it displays posts or Post Types that were published during any given month. The<div class=\"read-more\"><a class=\"btn button-secondary\" href=\"https:\/\/themespress.ca\/en\/wordpress-display-posts-of-the-month-with-get_posts\/\">Read More<\/a><\/div>","protected":false},"author":81,"featured_media":5045,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[283],"tags":[322],"class_list":["post-5041","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-query"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/5041","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=5041"}],"version-history":[{"count":0,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/5041\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media\/5045"}],"wp:attachment":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media?parent=5041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/categories?post=5041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/tags?post=5041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}