Question

I know I can set a static page as the homepage but is it possible to set a single custom post as the site front page?

I created a custom post type called "portfolio" where users can add all the work they have done as posts and I need one of those posts to display as the homepage, exactly as if you would set a static front page in the reading settings.

Thanks in advance!

Was it helpful?

Solution

You don't want a post to be the front page, you want a custom post type entry to be the front page. Now that we have the terminology right, yes it's possible.

A client once asked me to do the exact same thing. They had a custom post type they needed displayed on the front page. Doing so was as simple as adding a filter to allow them to select a "stack" (their custom post type) from the reading page:

function add_pages_to_dropdown( $pages, $r ){
    if ( ! isset( $r[ 'name' ] ) )
        return $pages;

    if ( 'page_on_front' == $r[ 'name' ] ) {
        $args = array(
            'post_type' => 'portfolio'
        );

        $portfolios = get_posts( $args );
        $pages = array_merge( $pages, $portfolios );
    }

    return $pages;
}
add_filter( 'get_pages', 'add_pages_to_dropdown', 10, 2 );

Then it's just a matter of styling your templates to use the data correctly.

OTHER TIPS

There are many ways to accomplish this, though some are more advanced than others:

  1. Mark the blog post as sticky, and then set Posts per Page to 1 (Dashboard -> Settings -> Reading)
  2. Create a custom front-page.php template, and query the post in question, either via the sticky post designation, or via custom post meta
  3. Create a custom front-page.php template, and create a dynamic sidebar (i.e. Widget area), in which you add a Widget to display the post in question
  4. (Insert lots of other methods here...)

But I have to ask: why not just put that blog post content in a static Page, and then assign that static Page as the Front Page?

Edit

Based on your question clarification:

Well it's a client's requirement, I created a custom post type called "portfolio" where he adds all the work he's done and he just wants one of those posts to display as the homepage, exactly as if you would set a static front page in the reading settings, updating question.

You would need to use one of the following methods:

  1. Filter the page_on_front dropdown, as @EAMann suggests
  2. Create a front-page.php template file, that queries the correct Portfolio post, via custom post meta or other means

How about setting a custom meta field for "Use as Homepage", then query for that meta and get the associated post(s) and output them...naturally all of this would be in front-page.php

EAManns answer is good but does nothing if there are no pages at all in the database (since the option to choose static front pages is hidden if no pages are created). This is my attempt to fix this:

add_filter('get_pages', function ($pages, $r) {
    if (function_exists('get_current_screen') && 'options-reading' === get_current_screen()->id) {
        $pages = array_merge($pages, get_posts(['post_type' => 'area']));
    }

    return $pages;
}, 10, 2);

(Where 'area' should be substituted with the name of your custom post type.)

Have tried solution by EAMann it works, but as fiskhandlarn noticed it doesn't work if there are no pages at all. And his solution didn't worked for me. So I suggest another solution may be too straightforward, but it do the work.

add_filter( 'get_pages', 'add_pages_to_dropdown', 10, 2 );
function add_pages_to_dropdown( $pages, $r ){
    if ( ! isset( $r[ 'name' ] ) ) {
        $r['name'] = 'page_on_front';
    }
    if ( 'page_on_front' == $r[ 'name' ] ) {
        $args = array(
            'post_type' => 'device'
        );
        $portfolios = get_posts( $args );
        $pages = array_merge( $pages, $portfolios );
    }
    return $pages;
}

You can create new static page, create new template for this page and in template add

header("Location: https://your-site.com/url-of-post-for-main-page/");
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top