WordPress.org

Ready to get started?Download WordPress

Forums

Custom post type archive as front page (3 posts)

  1. leemon
    Member
    Posted 2 years ago #

    Hi!

    Is there any easy way to setup a custom post type archive as the front page in WP? I know I can create a template that modifies the post query to retrieve custom posts instead of normal posts, but I'm wondering if there's a simpler way to do this.

    Thanks in advance

  2. s_ha_dum
    Member
    Posted 2 years ago #

    Probably not. You could create a "Page" with the same name as your custom post archive slug, set that as the static front page, and see if it doesn't confuse things in the just the right way :)

    You will get better performance if you interrupt the main query with the post_* filters rather than create a new query.

  3. Marco Panichi
    Member
    Posted 1 year ago #

    Put this code (after replacing <MYTYPE> with you cpt name) into your functions.php:

    function add_<MYTPE>_to_dropdown( $pages, $r )
    {
    	if('page_on_front' == $r['name'])
    	{
    		$args = array(
    			'post_type' => '<MYTYPE>'
    		);
    		$items = get_posts($args);
    		$pages = array_merge($pages, $items);
    	}
    
    	return $pages;
    }
    add_filter( 'get_pages', 'add_<MYTPE>_to_dropdown' );
    
    function enable_front_page_<MYTPE>( $query )
    {
    	if('' == $query->query_vars['post_type'] && 0 != $query->query_vars['page_id'])
    		$query->query_vars['post_type'] = array( 'page', '<MYTPE>' );
    }
    add_action( 'pre_get_posts', 'enable_front_page_<MYTPE>' );

Topic Closed

This topic has been closed to new replies.

About this Topic