[Resolved] Custom post type category archive as a homepage

Home Forums Support [Resolved] Custom post type category archive as a homepage

Home Forums Support Custom post type category archive as a homepage

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #571308
    George

    Ok, this thing is doing my head in.

    I currently have a custom post type category archive as my homepage by essentially setting a redirect from an “empty” home page that I have created on the backend to the actual archive’s slug. It works fine but I am getting warnings from Google page Speed that this is not a good practice and that it could cause unnecessary delays on the homepage loading time. At the same time, I like the Load More functionality that I have on the blog layout and the fact that I can also search so I don’t want to use WP Show Posts just to display the posts.

    I tried this plugin Display Posts Shortcode plugin but I think it’s just using it’s own template to list the blog items. Is there a shortcode I could use that would just list the category archive as it is currently set in the Customizer settings?

    #571536
    David
    Staff
    Customer Support

    Hi George, Tom may have a snippet to fix this, there is this plugin that may be worth a try.

    https://wordpress.org/plugins/front-page-custom-post-type/

    #571562
    George

    Thanks David but with this plugin functionality, I lose the blog page, comes out blank.

    #571567
    David
    Staff
    Customer Support

    OK scrap that then! lol I think it might be best to get Tom’s input

    #571986
    Tom
    Lead Developer
    Lead Developer

    So what if we had a function which would make it so your posts page (Settings > Reading) only displayed posts from that one custom post type? Would that work for you?

    #572098
    George

    Eh, no because the blog page already displays the normal posts.

    #572298
    George

    Ok Tom, I think I’ve sorted that. I found some code online that loads a custom post archive on the homepage:

    /**
     * Load custom post type archive on home page
     *
     * Reference: http://www.wpaustralia.org/wordpress-forums/topic/pre_get_posts-and-is_front_page/
     * Reference: http://wordpress.stackexchange.com/questions/30851/how-to-use-a-custom-post-type-archive-as-front-page
     */
    function prefix_downloads_front_page( $query ) {
    
        // Only filter the main query on the front-end
        if ( is_admin() || ! $query->is_main_query() ) {
        	return;
        }
    
        global $wp;
        $front = false;
    
    	// If the latest posts are showing on the home page
        if ( ( is_home() && empty( $wp->query_string ) ) ) {
        	$front = true;
        }
    
    	// If a static page is set as the home page
      
        if ( ( $query->get( 'page_id' ) == get_option( 'page_on_front' ) && get_option( 'page_on_front' ) ) || empty( $wp->query_string ) ) {
        	$front = true;  
        }
    
        if ( $front ) :
      
            $query->set( 'post_type', 'video' );
            $query->set( 'page_id', '' );
    
            // Set properties to match an archive
            $query->is_page = 0;
            $query->is_singular = 0;
            $query->is_post_type_archive = 1;
            $query->is_archive = 1;
    
        endif;
    
    }
    add_action( 'pre_get_posts', 'prefix_downloads_front_page' );

    I just had to apply the style for the category to the archive but it seems to be working.

    Thanks!

    #572530
    Tom
    Lead Developer
    Lead Developer

    Awesome 🙂

    #580174
    George

    Hi Tom, sorry for opening this up again. Can you plese have a look at the above code I provided and tell me why the Load More keeps loading the same group of items every time? The code is supposed to load the video custom post category archive on the homepage so you will only be able to see the issue when you hit the logo at the top and then clicking the Load More at the bottom.

    #580454
    Tom
    Lead Developer
    Lead Developer

    Hmm, what happens if you go to yourblog.com/page/2? Does it load the same posts as the first page?

    #580577
    George

    Yes, it loads the same posts as the first page:
    https://bit.ly/2kacN3g

    #580947
    Tom
    Lead Developer
    Lead Developer

    Then there’s definitely something broken with your function.

    What if you just did this?:

    function prefix_downloads_front_page( $query ) {
    
        // Only filter the main query on the front-end
        if ( is_admin() || ! $query->is_main_query() ) {
        	return;
        }
        
        if ( is_home() ) {
            $query->set( 'post_type', 'video' );
        }
    
    }
    add_action( 'pre_get_posts', 'prefix_downloads_front_page' );
    #581114
    George

    It loads an empty page(which the Video page is anyway). With the previous function at least the video custom post type category archive was loaded on the page when clicking the logo homepage. I think in my case I need to use is_front_page() instead of is_home() (don’t want it to load it on the blog home page)but it doesn’t work either.

    #581334
    Tom
    Lead Developer
    Lead Developer

    Ah, so the front page isn’t set as the posts page. In that case pagination probably isn’t set up.

    I found this page: https://github.com/10up/ElasticPress/issues/222

    Perhaps try adding this part to your function?:

    //Fix pagination
    if ( get_query_var('paged') ) {
        $paged = get_query_var('paged');
    } elseif ( get_query_var('page') ) {
        $paged = get_query_var('page');
    } else {
        $paged = 1;
    }
    
    $query->set( 'paged', $paged );
    #581508
    George

    Oh that was sweet, Tom! Works fine now man. Thanks again!

Viewing 15 posts - 1 through 15 (of 18 total)
  • You must be logged in to reply to this topic.