WP custom post types pagination/404 error

I’m using a custom post type in WordPress (called “ns_news_article” generated from Magic Fields) which are registered and working. This rewrites the URL to add “category/news” after the domain (so: www.mydomain.com/category/news/custom-post-1). I have to have the “fake directories” in there.

I have this in the functions.php:

Read More
add_rewrite_rule("[ns_news_article]" . '$', "index.php?pagename=[ns_news_article]", "top");
add_rewrite_rule("[ns_news_article]" . '/page/([0-9])*/?', "index.php?pagename=[ns_news_article]" . '&paged=$matches[1]', "top"); 
global $wp_rewrite;
$wp_rewrite->flush_rules();

The 2nd line takes care of pagination 404 problem when viewing as multiple pages of lists, but then gives 404 errors when trying to view the single post. If I get rid of this, you can view the posts, but pagination doesn’t work.

Can I fix this or should it be done another way?

Related posts

Leave a Reply

1 comment

  1. I’ve recently done custom post types pagination, so this is how I’ve done. Let’s say your custom post type it’s called “customp”.

    1. Create a file in your theme called page-customp.php. Then publish an empty page with “Customp” title. Now, when you’re visiting http://www.yourdomain.com/customp you will see a page that is using page-customp.php as a template. Now we will be using this page to show the custom posts and with a pagination.

    2. Place this code in your page-customp.php file:

      $paged = 1;
      
      $postsPerPage = 5;
      
       if ( get_query_var('paged') ) $paged = get_query_var('paged');
      
       if ( get_query_var('page') )  $paged = get_query_var('page');
      
       query_posts( '&post_type=customp&paged=' . $paged . '&posts_per_page='.$postsPerPage );
      
      while ( have_posts() ) : the_post();
      
        the_title();
        echo get_the_excerpt();
      
      endwhile;
      
      customp_paginate();
      
    3. In your functions.php file place this:

      function customp_paginate() {
      
        global $wp_query, $wp_rewrite;
      
        $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
      
        $pagination = array(
          'base' => @add_query_arg('page', '%#%'),
          'format' => '',
          'total' => $wp_query->max_num_pages,
          'current' => $current,
          'show_all' => true,
          'type' => 'list',
          'prev_text' => '«',
          'next_text' => '»',
        );
      
        if ($wp_rewrite->using_permalinks())
          $pagination['base'] = user_trailingslashit(trailingslashit(remove_query_arg('s', get_pagenum_link(1))) . 'page/%#%/', 'paged');
      
        if (!empty($wp_query) && $pagination['total'] != 1) {
          $pagination['add_args'] = array('s' => get_query_var('s'));
      
          echo "<div class='paginate'><strong>Pages:</strong>" . paginate_links($pagination) . "</div>";
        }
      
      }
      

    I hope it helps 🙂