title: WordPress pagination links return 404 error on custom post types (only)?
body: <p>When I click a pagination link in my custom post type &quot;works&quot; listing of posts (or enter a URL like &quot;mysite.com/works/page/2&quot;), I get the 404 page.</p>
  <p>Regular post type pagination (blog) works correctly. Plus, everything else regarding my custom post type &quot;works&quot; seems to work fine.</p>
  <p>My <code>template-works.php</code> file has this:</p>
  <pre class="lang-php prettyprint-override"><code>    get_home_pagination();
          
      $args = array( 'post_type' =&gt; 'works', 'posts_per_page' =&gt; 10, 'paged' =&gt; $paged ); $wp_query = new WP_Query( $args );
      
      if ( $wp_query-&gt;have_posts() ) :
          while ($wp_query-&gt;have_posts()) :
              $wp_query-&gt;the_post();
              get_template_part( 'loop', 'works' );
          endwhile;      
      else:
          _e( 'Nothing here.' );
      endif;
  </code></pre>
  <p>The same code (replacing the post-type, of course) is used in the <code>template-blog.php</code>, where pagination links work correctly.</p>
  <p>And then in <code>functions.php</code> I've got:</p>
  <pre class="lang-php prettyprint-override"><code>function get_home_pagination() {
   
      global $paged, $wp_query, $wp;
      $args = wp_parse_args($wp-&gt;matched_query);
  
      if ( !empty ( $args['paged'] ) &amp;&amp; 0 == $paged ) {
          $wp_query-&gt;set('paged', $args['paged']);
          $paged = $args['paged'];
      }
  }
  </code></pre>
  <p>For a moment I thought that was happening because WordPress couldn't find any post within the loop: it tried to get the post ID when there were no post, as in line 29 of <code>post-template.php</code>. But then I realized this:</p>
  <p>If I type, for example, the URL &quot;(...)/blog/page/456&quot;, the blog template loads with the message &quot;Nothing here&quot;. No 404 error page.</p>
  <p>On the other hand, the URL &quot;works/page/2&quot;, which I expect to return some posts, ends on a 404 error page.</p>
  <p>Why?</p>
  <p><strong>EDIT:</strong></p>
  <p>On the 404 page I also get this error:</p>
  <blockquote>
  <p>Trying to get property of non-object in
  (…)/wordpress/wp-includes/post-template.php on the line 29.</p>
  </blockquote>
  <p>...but it might not be related to the pagination problem. I managed to remove the code which causes this error (from a plug-in I customized) and the pagination still doesn't work.</p>
  <p><strong>EDIT:</strong></p>
  <p>This is what I got by debugging the URL <em>mysite.com/trabalhos/pagina/2</em> with the Debug-this plug-in. It seems to be very wrong:</p>
  <p><em>Obs: trabalhos = works, pagina = page</em></p>
  <blockquote>
  <p>Matched Rule: trabalhos/([^/]+)(/[0-9]+)?/?$</p>
  <p>Matched Query: works=pagina&amp;page=%2F2</p>
  <p>Query String:
  page=%2F2&amp;name=pagina&amp;post_type=works&amp;works=pagina&amp;debug-this=rewrites</p>
  </blockquote>
  <p>In my understanding, the URL <em>mysite.com/trabalhos/pagina/2</em> SHOULD point to the following URL (except it doesn't work - but If i replace 'trabalhos' with 'noticias' - my slug for 'blog' - it works):</p>
  <blockquote>
  <p>mysite.com/trabalhos/pagina/2/?pagename=trabalhos</p>
  </blockquote>
  <p>However, the URL <em>mysite.com/trabalhos/pagina/2</em> points to this weird url:</p>
  <blockquote>
  <p>mysite.com/trabalhos/pagina/2?page=%2F2&amp;works=pagina&amp;post_type=works&amp;name=pagina</p>
  </blockquote>
  <p>And surprisingly, the following URL points exactly to the content I was expecting, that is, page 2 of works (trabalhos):</p>
  <blockquote>
  <p>mysite.com/noticias/pagina/2/?pagename=trabalhos</p>
  </blockquote>
  <p>I seem to be close to the solution...however I tried deactivating all plug-ins, removing almost all theme php code, each time reseting the permalinks settings in admin panel, among other things, and the error persists.</p>
  <p>Is it some misconfiguration? Where would it be?</p>
  <p>Thanks in advance for any help!</p>