-1

I'm trying to set up a custom post type single post as a homepage. I have custom plugin, it registers CPT called STACKS, I'm using it to build various one page scrolling pages. It has its own custom template etc.. Everything is working fine. However, I'm not able to choose the post from STACKS as a homepage.

Here is the part of the code where I register custom post type, I don't know, maybe args and rewrite rules are very important in this specific situation, to be able to choose CPT post as a homepage from reading options in backend.

$rewrite = array(
    'slug'                  => 'stacks',
    'with_front'            => true,
    'pages'                 => true,
    'feeds'                 => true,
);
$args = array(
    'label'                 => __( 'Stacks', 'Stacks' ),
    'labels'                => $labels,
    'supports'              => array( 'title', ),
    'hierarchical'          => true,
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 20,
    'menu_icon'             => 'dashicons-admin-post',
    'show_in_admin_bar'     => false,
    'show_in_nav_menus'     => true,
    'can_export'            => true,
    'has_archive'           => false,       
    'exclude_from_search'   => false,
    'publicly_queryable'    => true,
    'rewrite'               => $rewrite,
    'capability_type'       => 'page',
);
register_post_type( 'stacks', $args );

I found various sources from Google and Stack Exchange.

I guess these answers worked on previous WordPress versions, but they don't work now.

I used this code as seen on the pages from Stack Exchange.

function wpa18013_add_pages_to_dropdown( $pages, $r ){
    if('page_on_front' == $r['name']){
        $args = array(
            'post_type' => 'stacks'
        );
        $stacks = get_posts($args);
        $pages = array_merge($pages, $stacks);
    }

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

When I add the code above to my functions.php in my plugin, I'm able to choose the CPT post as a homepage from reading options in backend. However, when I go to homepage, Url is 127.0.0.1/website/stacks/stackhome instead of 127.0.0.1/website

The second part of the code given in these Stack Exchange answers, doesn't work for me. Code which is supposed to take care of the added URL path from custom post type... When I add this code to functions.php and go to homepage, url path from CPT is gone, but so is the custom post type content and template... it just displays the regular header, blank primary content section and the footer from the wp enabled theme.

function enable_front_page_stacks( $query ){
    if('' == $query->query_vars['post_type'] && 0 !=     $query->query_vars['page_id'])
    $query->query_vars['post_type'] = array( 'page', 'stacks' );
}
add_action( 'pre_get_posts', 'enable_front_page_stacks' );
0

There is a lot of nuance here, depending on what exactly you want to accomplish. I don't feel confident I completely grasp it from your question.

In general WP logic flow is following:

  • URL is converted to query vars
  • query vars go into main query instance
  • main query instance queries posts
  • template loader uses conditional tags (which use main query) to determine template
  • template runs loop over main query

You seem to want two things: to have home page display CPTs and have home page use special template for them. So there are two things you need to adjust: posts queried and template used.

Basic implementation might look like this:

class Stacks {

    public function __construct() {

        add_action( 'init', array( $this, 'init' ) );
        add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
        add_action( 'template_include', array( $this, 'template_include' ) );
    }

    public function init() {

        // your registration code
    }

    public function pre_get_posts( $wp_query ) {

        if ( $wp_query->is_main_query() && $wp_query->is_home() ) {
            $wp_query->set( 'post_type', 'stacks' );
        }
    }

    public function template_include() {

        if ( is_home() ) {
            return get_stylesheet_directory() . '/stacks.php';
        }
    }
}

$stacks = new Stacks();

Note that CPTs are not created equal with native post types. Despite similar mechanics native types are "special" and there are often quirks and behaviors specific to them when you want to diverge from default operation.

  • Hi Rarst, Thank you for the response. Im sorry, I think I wasnt clear in my question. I dont want to display CPTs on frontpage, I could do that with custom page template and custom query. I want to be able to choose Custom Post Type posts as a homepage, as you would do it with any regular pages in backend. Settings >> Reading section. Ultimately, I was able to choose the CPT post as a homepage from the dropdown in reading section. But the homepage URL gets that additional url path of CPT – Domeniko Jun 11 '16 at 19:04
  • Besides that, I dont want to force the template on homepage.. In my scenario, customer should be able to choose if he wants to set stack page as a homepage or use regular page for frontpage. Another thing is, all this STACKS and related functionality are coming from custom plugin, not in the theme. – Domeniko Jun 11 '16 at 19:07
  • If you can check the links I posted to other stackoverflow questions, you'll see exactly what my issue is, however, the solution given on these 2 links, does not work anymore, I guess they were good for older WP versions. – Domeniko Jun 11 '16 at 19:09
  • "choose Custom Post Type posts as a homepage" I completely don't follow what you mean by this and which "regular" functionality you refer to. – Rarst Jun 11 '16 at 19:12
  • 1
    Hmmm... Ok. So you mean you want CPT post as static front page? I would say noooope, that stuff is crazy and hardcoded all over core. Some other soul might be braver than me, but I wouldn't even try. – Rarst Jun 11 '16 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.