I'm writing a squeeze page plugin and I've hit a bump. The plugin needs to be able to display custom post types with a custom template and also be able to be used for the homepage.
So I've got the code to set a template for the custom post type:
add_filter('single_template', 'my_custom_template');
function my_custom_template($single) {
global $wp_query, $post;
if ($post->post_type == "squeeze_page"){
if(file_exists(ABSPATH . '/wp-content/plugins/wpsqueezr/squeeze_page.php'))
return ABSPATH . '/wp-content/plugins/wpsqueezr/squeeze_page.php';
}
return $single;
}
That works fine for displaying the custom post type. Adding it to the Reading settings works:
add_filter( 'get_pages', 'add_my_cpt' );
function add_my_cpt( $pages )
{
$my_cpt_pages = new WP_Query( array( 'post_type' => 'squeeze_page' ) );
if ( $my_cpt_pages->post_count > 0 )
{
$pages = array_merge( $pages, $my_cpt_pages->posts );
}
return $pages;
}
Now here's where it gets messy. That all works fine, but if you set the custom post type to the homepage, it redirects to the custom post type page, no good.
I found this code which adds the custom post type to the frontpage, but it doesn't show the custom template I've setup for single pages:
function enable_front_page_stacks( $query ){
if(( ! isset($query->query_vars['post_type']) || '' == $query->query_vars['post_type']) && 0 != $query->query_vars['page_id'])
$query->query_vars['post_type'] = array( 'page', 'squeeze_page' );
}
add_action( 'pre_get_posts', 'enable_front_page_stacks' );
I need to show the custom template for the custom post type on the homepage, not the custom post type in the homepage template. I tried the home_template filter but that didn't do anything.
Any ideas?
Edit: Worked it out, just add a page_template filter as well as a single_template filter. I hope this helps someone else! Cheers!