Support » Plugins » Hacks » Paginated Custom Post Type Archive: 404 Error

  • Resolved tnoguchi


    I’ve been getting a 404 error on a CPT archive page, and am not sure why. I can go to the page set for the custom post type archive, but the next page /mycustomposttype/page/2 gets a 404 error.

    I don’t think that there’s anything wrong with my code and I’ve flushed my permalink rules and I think that the CPT is registered correctly. But my code is below. Any insights would be greatly appreciated since I can’t see why the pages are not being found.

    <!-- Row for main content area -->
        <div class="full-width" role="main" >
         <section id="sketchbook-wall">
           <ul id="sketchbook-container" class="large-block-grid-4">
                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                $args = array(
                  'post_type' => 'tn_cstm_sketchbook',
                  'orderby' => 'menu_order',
                  'order' =>  'ASC',
                  'posts_per_page' => 2,
                  'paged'=> $paged
                  $loop = new WP_Query($args);
                  while($loop->have_posts()) : $loop->the_post(); ?>
                    <figure class="sketch-thumb">
                       if(get_field('sketch')) {
                        $sketch = get_field('sketch');
                        $large_image = wp_get_attachment_image_src( $sketch, 'full' );
                        $thumbnail = wp_get_attachment_image_src( $sketch, 'large' );
                        $attachment = get_post( get_field('sketch') );
                        $alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
                    <a href="<?php echo $large_image[0]; ?>" data-id="<?php the_ID(); ?>"  class="reveal" >
                      <img src="<?php echo $thumbnail[0]; ?>" alt="<?php echo $alt; ?>" />
                <?php endwhile; ?>
            // Bring $wp_query into the scope of the function
            global $wp_query;
            // Backup the original property value
            $backup_page_total = $wp_query->max_num_pages;
            // Copy the custom query property to the $wp_query object
            $wp_query->max_num_pages = $loop->max_num_pages;
            <!-- now show the paging links -->
            <div class="alignleft"><?php next_posts_link('Next Entries'); ?></div>
            <div class="alignright"><?php previous_posts_link('Previous Entries'); ?></div>
            // Finally restore the $wp_query property to it's original value
            $wp_query->max_num_pages = $backup_page_total;
         </div>  <!-- End Content -->
            // Register Custom Post Taxonomy
             public  function create_work_taxonomies() {
                            'labels' => array(
                                'name' => 'Sketchbook Categories',
                                'add_new_item' => 'Add New Sketchbook Category',
                                'new_item_name' => "New Sketchbook Category"
                            'show_ui' => true,
                            'show_in_nav_menus' => true,
                            'show_admin_column' => true, //Show custom taxonomy in admin columns
                            'show_tagcloud' => false,
                            'hierarchical' => true,
                            'args' => array( 'orderby' => 'term_order' ),
                            'rewrite' => array('slug' => 'sketchbook-categories', 'with_front' => false )
            // Register Custom Post Type
               public function create_sketchbook_work() {
                    register_post_type( 'tn_cstm_sketchbook',
                            'labels' => array(
                                'name' => 'Sketchbook',
                                'singular_name' => 'Sketchbook Page',
                                'add_new' => 'Add New Sketchbook Page',
                                'add_new_item' => 'Add New Sketchbook Page',
                                'edit' => 'Edit Sketchbook Page',
                                'edit_item' => 'Edit Sketchbook Page',
                                'new_item' => 'New Sketchbook Page',
                                'view' => 'View Sketchbook Page',
                                'view_item' => 'View Sketchbook Pages',
                                'search_items' => 'Search Sketchbook Pages',
                                'not_found' => 'No Sketchbook Pages found',
                                'not_found_in_trash' => 'No Sketchbook Pages Found in Trash',
                                'parent' => 'Parent Sketchbook Page'
                            'public' => true,
                            'show_in_menu' => true,
                            'menu_position' => 29,
                            'taxonomies' => array( '' ),
                            'menu_icon' => plugins_url( 'images/sketchbook-icon.png', __FILE__ ),
                            'publicly_queryable' => true,
                            'query_var' => true,
                            'rewrite' => array( 'slug' => 'sketchbook', 'with_front' => false ),
                            'has_archive' => true,
                            'hierarchical' => false,
                            'supports' => array( 'title', 'page-attributes' )
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz


    I could be wrong, because pagination makes my head spin, but it appears there’s mismatch between what the main query is doing and what your local query is doing. You’ve fooled the next_posts_link() into generating a page 2 link, but the main query really has no page 2, thus the 404.

    I think you need to either alter the main query to meet you needs using the ‘pre_get_posts’ action or manage the page number of your local query yourself, using get_pagenum_link() to generate the nav links. Trying to fool next_posts_link() into paginating your local query is not worth the effort. If you use ‘pre_get_posts’, the nav functions will work correctly without having to do anything special.

    Hi bcworkz, thank you for your thoughtful insights. I think of all WP things, permalinks and pagination have to be the two things that I find maddeningly complex.

    I think that my approach was probably the issue, like you said. Overriding the default query for archives is far too complex.

    As a solution, I ended up creating a custom page template from the above custom query and assigned it to a page that I created.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Paginated Custom Post Type Archive: 404 Error’ is closed to new replies.