Search code examples
wordpresspaginationhttp-status-code-404custom-post-typetaxonomy

Pagination custom post in author.php not found in second page


I created Blog type by this code

 add_action( 'init', 'create_posttype' );
 function create_posttype() {
       register_post_type( 'blog',
       array(
      'labels' => array(
      'name' => __( 'Blog' ),
      'singular_name' => __( 'Blog' ),
      'add_new' => __('Add New'),
      'add_new_item' => __('Add New Blog'),
      'edit_item' => __('Edit Blog'),
      'new_item' => __('New Blog'),
      'view_item' => __('View Blog'),
      'search_items' => __('Search Blog'),
      'not_found' =>  __('No blog found'),
      'not_found_in_trash' => __('No blog found in Trash')
   ),
     'public' => true,
     'has_archive' => true,
     'rewrite' => array('slug' => 'blog','with_front'=>FALSE),
     'supports' => array('title','author', 'blogg', 'editor', 'excerpt', 'thumbnail', 'comments'           )
    )
);
    $parent_term = term_exists( 'Blogg', 'blogg' ); // array is returned if taxonomy is given
    if(!$parent_term){

        wp_insert_term( 'Blogg','blogg',array('slug' => 'blogg'));
    }
  }

Then, I added a taxonomy as follow

   register_taxonomy("blogg", array("blog"),
        array("hierarchical"=> true, "label" => "Blog Category", "singular_label" => "Blog", 
        "rewrite"=> true,'query_var' => true,'taxonomies'=>array('post-tag','blogg')));

Now, in author.php I would like to list all blogs is writed by the author with a pagination. My code in author.php is:

 <?php
 $author_id    = get_query_var( 'author' );
 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
 $args = array(
    'author' => $author_id,
    'post_type' => 'blog',
    'post_status' => 'publish',
    'posts_per_page' => 10,     
    '$paged' =>$paged
);
query_posts($args );
  if ( have_posts() ) :
   while (have_posts() ) : the_post();
           echo somethings here......
   endwhile;
  ?>
  <div class="navigation">
                  <div class="alignleft"><?php previous_posts_link('&laquo; Forrige') ?></div>
                  <div class="alignright"><?php next_posts_link('Mer &raquo;') ?></div>
                </div>
 <?php endif;?>

Pagination is shown, but when I go to second page with url http://myweb.no/author/myname/page/2 It's not found. I tried some solutions here https://wordpress.org/tags/custom-post-type-pagination But it still not work. Please help me. Thanks advance!


Solution

  • You have two major problems here

    • You should never use query_posts. It is just a troublesome bad function which likes to fail on pagination

    • Never change the main query with a custom query. Rather modify the query variables just before the main query is executed by means of pre_get_posts

    You should read and work through this post I have recently done here on SO. I have explained all the do's and don't there, so you should really spend some time on that :-)

    To solve your issue, delete your custom query in author.php and revert back to the main loop. You should only have something like the following in your author.php

    if(have_posts()) {
      while(have_posts()) {
         the_post();
    
         //your loop elements
    
      }
    }
    

    Now, in your functions.php, add the following code. This will add your custom post type to your author pages

    function author_cpt_filter($query) {
        if ( !is_admin() && $query->is_main_query() ) {
          if ($query->is_author()) {
            $query->set('post_type', array('post', 'blog'));
            $query->set('post_per_page', 10);
    
          }
        }
    }
    
    add_action('pre_get_posts','author_cpt_filter');
    

    You can now see your custom post type posts in your author pages, and you can also paginate it normally like you would on your homepage