• I can’t seem to get WordPress to show the proper posts for my custom post type on an archive page when using next_posts_link("older posts");. I’m pretty new to WordPress so i’m not sure what I could be doing incorrectly.

    Essentially what i’m shooting for is to be able to go backward and forwards using the older posts link and newer posts link for my custom posts that I created.

    What happens is, although I am able to see and click on the older posts link (and debug that I do in fact have multiple pages), it keeps giving me a 404 error.

    I would really appreciate the help. Thanks in advance

    <?php
    
        $pageCount = 3;     //number of pages for our work section
    
        //set our post type to whatever we find out it is. So that later we can use that title to query
        //as well as grab the post count for that category
        $postType = '';
        if ( is_post_type_archive('illustrations') ) {
            $postType = 'illustrations';
        }else if(is_post_type_archive('digital-designs')){
            $postType = 'digital-designs';
        }else if(is_post_type_archive('weddings')){
            $postType = 'weddings';
        }else if(is_post_type_archive('lettering')){
            $postType = 'lettering';
        }else{
            $postType = 'logos';
        }
    ?>
    
    <?php //query_posts(array('post_type' =>  $postType));
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    query_posts( array(
    'post_type' => $postType,
    'posts_per_page' => 4,
    'paged'=>$paged
    ) ); ?>
    
    <?php get_header(); ?>
    
    <div class="container">
    
        <div class="row main-work-spacer">
            <?php
            $postCount = wp_count_posts($postType);
            $archImgCount = 0;
    
            //basic loop
            while (have_posts() ) : the_post();
    
            ?>
            <div class="col-md-4 col-xs-12">
                <!--this will diplay our link to our main image with a lightbox tag so that it works properly-->
                <a class="thumbnail" href="<?php
                $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
                echo $feat_image;
                ?>" rel="lightbox" data-lightbox-gallery="lightbox[gallery-1]">
    
                    <?php
                    //is there a featured image?
                    if( has_post_thumbnail() ) {
                        the_post_thumbnail( 'gallery-medium', array( 'class' => 'img-responsive, center-block' ) ); //we added the img-responsive and center-center block classes we will see though
                    }
                    ?>
                </a>
            </div>
    
            <?php
            $archImgCount++;
            //this line is to prevent an extra row from being added on our last item
            if($postCount->publish % 3 != 0 || $archImgCount < $postCount->publish){
                //if are on our third image start a new row
                if($archImgCount % 3 == 0){ ?>
    
                    </div>
                    <div class="row main-work-spacer">
    
            <?php }
            }
    
            endwhile; ?>
        </div>
    
    <!-- pagination occurs here -->
    <?php
        $additionPages = false;
        if($wp_query->max_num_pages > 1){
            $additionPages = true;
        }
        echo $wp_query->max_num_pages;
    
        //older posts shows up but it redirect to a 404 page
        next_posts_link("older posts");
    
        previous_posts_link("newer posts");
    ?>
        <nav>
            <ul class="pager">
                <li class="previous"><a href="#"><span aria-hidden="true">←</span> Older</a>
                <li class="next"><a href="#">Newer <span aria-hidden="true">→</span></a>
    
        </nav>
    
    </div>
    <?php get_footer(); ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter meatloaf1024

    (@meatloaf1024)

    So I seem to have figured out the issue. It seems that WordPress will not generate the pages for me unless my posts_per_page code >= the Blog pages show at most __ per page in the settings->reading section.

    I think this is because WordPress generates the pages based on what you tell it in the settings->reading->blog pages show at most. And if you don’t have enough pages already it wont work properly.

    For example if i have 8 posts and i set posts_per_page to 2 in my code, and in general->reading->blog_pages_show_at_most to 3 pages, WordPress will only generate 3 pages for me ( 8 posts / 3 per page = 2.6 pages rounds up to 3 pages). In fact though I needed 4 because i designated 2 posts_per_page in my code ( 8 posts / 2 per page = 4 pages for viewing). This is why when we view pages 1 – 3 everything works correctly, but when we go to the fourth page we get a 404.

    Is this how WordPress is suppose to behave?

    Thanks in advance and I hope I helped someone!

    Thread Starter meatloaf1024

    (@meatloaf1024)

    Ended up fixing this issue with this code in my functions.php script

    //this function will ensure we dont have to set our "blog pages show at most" count to <= our posts_per_page in our
    //archive code
    function portfolio_images_posts_per_page( $query ) {
        $postTypes = array("illustrations", "digital-designs", "weddings", "lettering", "logos" );
    
        foreach($postTypes as $postType){
            if ( $query->query_vars['post_type'] == $postType ) $query->query_vars['posts_per_page'] = 1;
        }
    
        return $query;
    }
    
    if ( !is_admin() ) add_filter( 'pre_get_posts', 'portfolio_images_posts_per_page' );
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘custom post type older posts give 404’ is closed to new replies.