Custom Post Type as front page

WordPress 5 posts 2 voices

  1. Hello Everybody,

    I have setup Portfolio page (single) as a frontpage for my website using the plugin https://wordpress.org/plugins/mpress-custom-front-page/

    Which works fine, but doesn’t make my said Portfolio page slug behave like the standard WordPress page slug.

    What I mean is,

    By default, if we setup some page as frontpage, its slug gets changed,

    let say its slug was “http://mywebsite.com/my-page” and after we set it as frontpage, WordPress changes it slug to “http://mywebsite.com”

    But in my case, as I am setting custom post type, this doesn’t happen.

    Can anybody help me with this?

    Note: I am using Portfolio plugin by Justin.

  2. You have a single portfolio project that is set to the front page? You could filter post_type_link to change the permalink.

    add_filter( 'post_type_link', 'my_ccp_post_type_link', 15, 2 );
    
    function my_ccp_post_type_link( $post_link, $post ) {
    
    	// Bail if this isn't a portfolio project.
    	if ( ccp_get_project_post_type() !== $post->post_type )
    		return $post_link;
    
    	if ( get_option( 'page_on_front' ) === $post->ID )
    		return home_url();
    
    	return $post_link;
    }

    That code assumes that the plugin filters the standard WP page_on_front option. If not, you’ll want to change get_option( 'page_on_front' ) to the ID of the portfolio project you have set on the front page.

  3. Hi Justin,

    Thanks for the code, I have already tried that, but for some reasons it doesn’t work.

    See screenshot, it shows the original url of project instead of homepage.

    https://cloud.githubusercontent.com/assets/1642796/21416516/597c2394-c835-11e6-88e2-319ee90d7f63.png

    Although if I try to access this url, it gets redirected to homepage successfully and shows the relevant portfolio project (single).

    Any other ideas?

  4. I experimented with it this time, considering you have provided the same code, so it must be correct.

    and also did some digging into WP Core code,

    it seems like if we use === it fail because $post->ID is string

    so WP Core handles it like this

    if ( intval( get_option( 'page_on_front' ) ) === $post->ID ) {}

    or

    if ( $post->ID == get_option( 'page_on_front' ) ) {}

    and I used first option and it works.

    Thanks

  5. Sorry for typo,

    get_option( ‘page_on_front’ ) returns ID as string not $post->ID