{"id":315,"date":"2013-09-18T23:23:21","date_gmt":"2013-09-18T15:23:21","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=315"},"modified":"2018-09-04T13:06:00","modified_gmt":"2018-09-04T05:06:00","slug":"how-i-create-a-page-in-my-wordpress-blog-that-lists-all-my-posts-and-groups-them-by-month-and-year","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-i-create-a-page-in-my-wordpress-blog-that-lists-all-my-posts-and-groups-them-by-month-and-year\/","title":{"rendered":"How I create a page in my WordPress blog that lists all my posts and groups them by month and year"},"content":{"rendered":"<p>I realised that there was something missing in my WordPress blog; a <a href=\"http:\/\/www.techcoil.com\/blog\/all-posts\" title=\"All posts in Techcoil blog\">page that lists all the posts that I had written so far<\/a>. This post documents what I had explored to give my blog a page that lists all my posts, starting from the most recent post down to the oldest post, and groups them by month and year.<\/p>\n<h2>A high level plan<\/h2>\n<p>I first did some research on this topic. In order to include a page that list all the posts in my WordPress blog, I will need to do the following:<\/p>\n<ol>\n<li>Create a template file in my theme folder, for instance, <code><strong>all-my-posts-so-far.php<\/strong><\/code><\/li>\n<li>Create a page and name it as \"<strong>All Posts<\/strong>\" and set the template file as <code><strong>all-my-posts-so-far.php<\/strong><\/code>.<\/li>\n<\/ol>\n<h2>The parts that form all-my-posts-so-far.php<\/h2>\n<p>I broke down the codes in all-my-posts-so-far.php into the following parts:<\/p>\n<ol>\n<li>Database access codes that retrieve all the published posts in my blog.<\/li>\n<li>Iteration codes that display the post.<\/li>\n<li>Conditional codes that group posts of the same month and year together.<\/li>\n<\/ol>\n<h3>The database access codes that retrieve  all the published posts in my blog<\/h3>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nglobal $wpdb;\r\n$allPostsArray = $wpdb-&gt;get_results(\r\n\t'SELECT ID, post_title, post_date, post_name FROM ' . \r\n         $wpdb-&gt;posts . \r\n        ' WHERE post_type=\\'post\\' AND post_status = \\'publish\\'' .\r\n        ' ORDER BY post_date desc'\r\n);\r\n<\/pre>\n<p>I globalize the <code>$wpdb<\/code> variable to gain code access to my WordPress database. I then call its <code>get_results<\/code> function, passing it a SQL select statement to get all my posts. The SQL statement looks for posts with the <code>post_type<\/code> column having a \"post\" value, the <code>post_status<\/code> column having a \"publish\" value, ordered by the <code>post_date<\/code> column in descending order. <\/p>\n<p>The <code>$wpdb->posts<\/code> variable is used to retrieve the actual name of the table in my WordPress database that stores all my posts.<\/p>\n<h3>Iteration codes that display the posts and conditional codes that group posts of the same month and year together<\/h3>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$earlierPostMonthYear = 'January 2000';\t\r\n$latestMonthVisited = FALSE;\r\nforeach($allPostsArray as $post) {\r\n    \r\n    $postDate = date_create($post-&gt;post_date);\r\n    $currentPostMonthYear = date_format($postDate, 'F Y');\r\n\t\t\r\n    $postFromAnotherMonthYear = ($currentPostMonthYear != $earlierPostMonthYear);\r\n\t\r\n    if ($postFromAnotherMonthYear) {\r\n\r\n        if (!$latestMonthVisited) {\r\n\t    $latestMonthVisited = TRUE;\r\n\t}\r\n\telse {\r\n\t    echo '&lt;\/ul&gt;';\r\n\t}\r\n?&gt;\r\n&lt;h3&gt;&lt;?php echo $currentPostMonthYear?&gt;&lt;\/h3&gt;\r\n&lt;ul&gt;\r\n&lt;?php\r\n\t} \/\/ end if ($postFromAnotherMonthYear)\t\r\n?&gt;\r\n&lt;li&gt;\r\n\t&lt;a href=&quot;&lt;?php echo get_bloginfo('url') . '\/' . $post-&gt;post_name;?&gt;&quot; \r\n           title=&quot;&lt;?php echo $post-&gt;post_title;?&gt;&quot;&gt;\r\n\t&lt;?php echo $post-&gt;post_title;?&gt;\r\n\t&lt;\/a&gt;\r\n&lt;\/li&gt;\r\n&lt;?php\r\n\t$earlierPostMonthYear = $currentPostMonthYear;\r\n    } \/\/ foreach\r\n?&gt;\r\n<\/pre>\n<p>I place the <code>$allPostsArray<\/code> variable in a <code>foreach<\/code> loop, setting each post record into the <code>$post<\/code> variable. I then use the <code>date_create<\/code> and <code>date_format<\/code> functions to get a string representation of the month and year of the current post. <\/p>\n<p>I then compare the month and year of the current post with the month and year of the previous post. When there is a difference, I do the following:<\/p>\n<ol>\n<li>Check if posts from the latest month had been visited, if yes, print a <a href=\"http:\/\/www.techcoil.com\/blog\/code-segments-for-rendering-html-4-0-pages\/\" title=\"Code segments for rendering html 4.0 pages\">HTML<\/a> unordered list end tag; if no, set <code>$latestMonthVisited<\/code> to <code>TRUE<\/code>.<\/li>\n<li>Print out the <a href=\"http:\/\/www.techcoil.com\/blog\/code-segments-for-rendering-html-4-0-pages\/\" title=\"Code segments for rendering html 4.0 pages\">HTML<\/a> unordered list start tag along side a HTML header level 3 element that enclosed the month and year of the current post.<\/li>\n<\/ol>\n<p>Outside the check, I print a HTML list element that enclosed the HTML link to the current post.<\/p>\n<h2>Create a WordPress page, name it as \"All Posts\", and link it to all-my-posts-so-far.php<\/h2>\n<p>Well the final step is straightforward. I hover to the \"Pages\" menu item at the left of my WordPress administrator site and click on the \"Add New\" link.<\/p>\n<p>I typed in \"All posts\" in the first text field, and the locate the Template dropdown at the \"Page Attributes\" section at the right. But wait...<\/p>\n<h4>Where is the option value for my all-my-posts-so-far.php WordPress template at the \"Page Attributes\" section?<\/h4>\n<p>It turned out that I had left something out at the start <code>all-my-posts-so-far.php<\/code>. <\/p>\n<p>To be able to select <code>all-my-posts-so-far.php<\/code> as the template file to link to the \"All posts\" page, I had to place the following code at the start of the file:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/*\r\nTemplate Name: All my posts so far\r\n*\/\r\n<\/pre>\n<p>With that, I completed the inclusion of a page that lists all the posts in my WordPress blog and groups them by month and year.<\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-55\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-55&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-55&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F315&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>I realised that there was something missing in my WordPress blog; a <a href=\"http:\/\/www.techcoil.com\/blog\/all-posts\" title=\"All posts in Techcoil blog\">page that lists all the posts that I had written so far<\/a>. This post documents what I had explored to give my blog a page that lists all my posts, starting from the most recent post down to the oldest post, and groups them by month and year.<\/p>\n","protected":false},"author":1,"featured_media":1217,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[13,5,170],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/WordPress-logo-on-black-background.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-55","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/315"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=315"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/315\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1217"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}