{"id":128933,"date":"2014-05-12T08:00:00","date_gmt":"2014-05-12T12:00:00","guid":{"rendered":"http:\/\/premium.wpmudev.org\/blog\/?p=128933"},"modified":"2014-05-13T20:29:04","modified_gmt":"2014-05-14T00:29:04","slug":"how-to-make-a-wordpress-post-not-page-your-home-page","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/how-to-make-a-wordpress-post-not-page-your-home-page\/","title":{"rendered":"How To Make A WordPress Post (Not Page!) Your Home Page"},"content":{"rendered":"<p>Making a WordPress Page your home page is easily done by using the Theme Customizer to select a static home page.<\/p>\n<p>But what about a post? How do you select a post as your home page?<\/p>\n<p>With a simple bit of coding that takes advantage of the WordPress chain of execution.<\/p>\n<figure id=\"attachment_128948\" class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"size-ratio-large wp-image-128948\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2014\/05\/front_page_post-700x235.jpg\" alt=\"Featured image of a post as a home page\" width=\"700\" height=\"235\" \/><figcaption class=\"wp-caption-text\">If you want to use a post as your home page then you&#8217;ll need to resort to coding.<\/figcaption><\/figure>\n<p><a title=\"Visit The Great Discontent\" href=\"http:\/\/thegreatdiscontent.com\/\" rel=\"noopener\" target=\"_blank\">The Great Discontent<\/a> is a great looking magazine that features fascinating &#8220;interviews on beginnings, creativity, and risk&#8221;.<\/p>\n<p>One of the design features that has always intrigued me about the site is that it uses the latest post as its home page. No messing around with the normal &#8216;gateway&#8217;, it&#8217;s straight into the most recent (long form) story.<\/p>\n<p>WordPress doesn&#8217;t have this functionality baked in. It&#8217;s very easy, via the theme customizer or the Reading Settings (Settings &gt; Reading) \u00a0to choose a static page as your home page, or a post list, but you cannot select a post as your home page.<\/p>\n<p>So, if you want to make a post your home you&#8217;ll need to resort to a bit of hacking but fortunately the code snippet is small. Add it either as a plugin (as provided) or simply take the function and add it to your functions.php.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction pfp_pre_get_posts ( $query ) {\r\n\r\n\/\/ only interested in home page and the main query\r\nif ( !$query-&gt;is_home() || !$query-&gt;is_main_query() ) return;\r\n\r\n\/\/ default args - most recent post\r\n$args = &#039;posts_per_page=1&amp;order=DESC&amp;orderby=date&amp;ignore_sticky_posts=true&#039;;\r\n\r\n\/\/ check for sticky posts\r\n$sticky = get_option( &#039;sticky_posts&#039; );\r\n\r\n\/\/ have sticky posts so use them\r\nif ( $sticky != &#039;&#039; ) {\r\n$args = &#039;p=&#039; . $sticky&#x5B;0];\r\n}\r\n\r\n\/\/ clear the current query\r\n$query-&gt;init();\r\n\r\n\/\/ parse in the new arguments\r\n$query-&gt;parse_query( $args );\r\n\r\n}\r\n\r\nadd_action( &#039;pre_get_posts&#039; , &#039;pfp_pre_get_posts&#039; );\r\n\r\n<\/pre>\n<p>All the code does is hook into the <a title=\"Read more about the pre_get_posts action on the WordPress Codex\" href=\"http:\/\/codex.wordpress.org\/Plugin_API\/Action_Reference\/pre_get_posts\" rel=\"noopener\" target=\"_blank\">pre_get_posts<\/a> action to reset the query retrieving the posts. It only does this if the page being output is the home page and this is the main query &#8211; this should leave your menus and widgets in tact.<\/p>\n<p>The function will first try to use any &#8216;sticky&#8217; posts; if there are none then it will simply grab the most recent post.<\/p>\n<p>The simplicity of the code is down to WordPress&#8217;s chain of events. The query gets altered before WordPress determines which template to use to display the page, so by changing the query to bring back just a single post it automatically selects the single post template for building the output.<\/p>\n<p>Of course, there are other ways to achieve the same outcome. You could use a static page and either embed a shortcode to pull in the content of a post or create a specific template to get the content; or you could make use of the WordPress template hierarchy and create a home.php file for your theme that pulled the appropriate content.<\/p>\n<p>But neither seem as elegantly simple as the plugin.<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Making a WordPress Page your home page is easily done but what a post? Here&#8217;s how to make a post your home page.<\/p>\n","protected":false},"author":262394,"featured_media":128948,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[263],"tags":[],"tutorials_categories":[],"class_list":["post-128933","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/128933","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/262394"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=128933"}],"version-history":[{"count":2,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/128933\/revisions"}],"predecessor-version":[{"id":194606,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/128933\/revisions\/194606"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/128948"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=128933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=128933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=128933"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=128933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}