2

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. I did some research here, and saw similar issues and tried to follow allow of the solutions presented. But no luck.

<!-- Row for main content area -->
<div class="full-width" role="main" >

 <section id="sketchbook-wall">


   <ul id="sketchbook-container" class="large-block-grid-4">
        <?php 

        $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(); ?>
          <li>
            <figure class="sketch-thumb">

            <?php 
               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; ?>" />

            </a> 
            <figcaption>

            </figcaption>

            </figure>

          </li>

        <?php endwhile; ?>

      </ul>


  <nav>
    <?php
    // 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>

    <?php
    // Finally restore the $wp_query property to it's original value
    $wp_query->max_num_pages = $backup_page_total;
    ?>
  </nav>


 </section>

 </div>  <!-- End Content -->


<?php

    // Register Custom Post Taxonomy
     public  function create_work_taxonomies() {
            register_taxonomy(
                'tn_cstm_sketchbook_plugin',
                'tn_cstm_sketchbook',
                array(
                    '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',
                array(
                    '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' )
                )
            );
        }

?>
3
  • 2
    Is there a reason you need a custom query there instead of just using the default for the CPT archive? Commented Jun 25, 2013 at 1:19
  • 2
    what @helgatheviking said. the issue is likely that the main query for your archive is loading the default number of posts, and that number exceeds the number of posts in that cpt, so the main query has no second page. there's no reason to run a custom query in the template, use the pre_get_posts action if you want to modify parameters of the archive main query, like posts_per_page.
    – Milo
    Commented Jun 25, 2013 at 1:50
  • helgatheviking, Milo. Thank you both for your insights. I'm building a custom gallery display for the CPT. Though, I think I understand more clearly the nature of why the custom query wasn't working. I think what I'll do is simply assign this to a page as a custom template. A lot more work seems to be involved in order to override the default query.
    – tnog
    Commented Jun 25, 2013 at 3:34

2 Answers 2

1

While pre_get_posts is a much better solution for modifying the parameters for the archive main query for specific conditions like Custom Post Types, as Milo and helgatheviking mentioned, trying to use a custom query to override the main query for an archive is overcomplicating the issue.

Since I'm building a custom Tumblr like gallery template that pulls in the featured post image linked through thumbnails on the page through AJAX and a modal pop-up window, what I ended up doing was using my custom query above in a custom template that I assigned for a page that I created. Going to the page, I get the pagination behavior that I wanted.

0

WordPress generates an archive page when your register the CPT. As stated you can modify posts per page from your functions file and register both the CPT and Taxonomy support using this code:

add_action( 'pre_get_posts', 'set_items' );
function set_items( $query ) {

    if( $query->is_main_query() && !is_admin() && is_post_type_archive( 'tn_cstm_sketchbook' ) ) {
        $query->set( 'posts_per_page', '12' );
    }



    // Register Custom Post Taxonomy
add_action( 'init', 'create_work_taxonomies' );               
function create_work_taxonomies() {
                register_taxonomy(
                    'tn_cstm_sketchbook_plugin',
                    'tn_cstm_sketchbook',
                    array(
                        '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
add_action( 'init', 'create_sketchbook_work' );        
function create_sketchbook_work() {
                register_post_type( 'tn_cstm_sketchbook',
                    array(
                        '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' )
                    )
                );
            }

No need for any custom query or CPT archive and you wouldn't use a template for the archive as WordPress uses the Template Hierarchy

You can then use CSS to display your featured images in a inline grid style.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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