Using ACF Options to set the WordPress homepage to anything I want

It’s weird what I don’t notice. In this case, I hadn’t noticed I couldn’t choose a custom post type as the homepage for WordPress. I could certainly make a page template or do some work arounds but I didn’t want to. There is an existing plugin out there called Front Type but I’m really trying to use the fewest possible plugins. Consider it a point of honor? Stupidity? Amusement?

Since I was already using Advanced Custom Fields Pro, I thought I could figure out a path.

I made an ACF options page and made a single post object field that I set to return the ID only. That lets me browse any post type and returns the ID.

The acf/save_post action lets me run the function whenever ACF saves. To keep that under control, I check to see if we’re on the options page by calling WP’s get_current_screen. I also make sure that it’s not already set correctly by comparing my ACF ID value to the get_option(‘page_on_front’) ID value.

//enable you to set the home page to be a custom post type via the ACF options page

function dlinq_set_home(){
	$screen = get_current_screen();
	if ($screen->id === "toplevel_page_dlinq-basics") {//the name of my acf option screen
		$post_id = get_field('choose_the_homepage', 'option');
		if($post_id != get_option('page_on_front')){//already the same?
			update_option( 'page_on_front', $post_id );
			update_option( 'show_on_front', 'page' );
		}
		
	}
}

add_action('acf/save_post', 'dlinq_set_home', 20);

Update 1

This path led to the homepage redirecting to the longer URL rather than staying on the base URL. I found a fix for that on good ol’ Stackoverflow.

//from https://wordpress.stackexchange.com/questions/18013/how-do-you-use-a-cpt-as-the-default-home-page/126271#126271
function dlqin_enable_front_page_cpt( $query ){
    if('' == $query->query_vars['post_type'] && 0 != $query->query_vars['page_id'])
        $query->query_vars['post_type'] = array( 'page', 'topic' );
}
add_action( 'pre_get_posts', 'dlqin_enable_front_page_cpt' );

Update 2

I then realized that messing with the reading preferences in the normal way had the chance to mess stuff up and I now had some non-standard behavior. That’s a bad move on my part. Solving problems to create problems.

So I went back to find a way to add custom post types to the normal WP front page drop down. I found this from back in 2013 . . . on Stackoverflow and it still works.

function dlinq_add_topics_to_dropdown( $pages ){
    $args = array(
        'post_type' => 'topic'//add your post type 
    );
    $items = get_posts($args);
    $pages = array_merge($pages, $items);

    return $pages;
}
add_filter( 'get_pages', 'dlinq_add_topics_to_dropdown' );

function dlinq_enable_front_page_topics( $query ){
    if('' == $query->query_vars['post_type'] && 0 != $query->query_vars['page_id'])
        $query->query_vars['post_type'] = array( 'page', 'topic' );//add your post type here
}
add_action( 'pre_get_posts', 'dlinq_enable_front_page_topics' );

Now the real question is whether any of this work was sensible.

Leave a Reply