WordPress pagination links return 404 error on custom post types (only)?
That's sounds like conflict of slug in your custom post type. Try to rename the post slug to any other name and go to permanent links again to regenerate rewrite rules.
zok
Updated on June 04, 2022Comments
-
zok almost 2 years
When I click a pagination link in my custom post type "works" listing of posts (or enter a URL like "mysite.com/works/page/2"), I get the 404 page.
Regular post type pagination (blog) works correctly. Plus, everything else regarding my custom post type "works" seems to work fine.
My
template-works.php
file has this:get_home_pagination(); $args = array( 'post_type' => 'works', 'posts_per_page' => 10, 'paged' => $paged ); $wp_query = new WP_Query( $args ); if ( $wp_query->have_posts() ) : while ($wp_query->have_posts()) : $wp_query->the_post(); get_template_part( 'loop', 'works' ); endwhile; else: _e( 'Nothing here.' ); endif;
The same code (replacing the post-type, of course) is used in the
template-blog.php
, where pagination links work correctly.And then in
functions.php
I've got:function get_home_pagination() { global $paged, $wp_query, $wp; $args = wp_parse_args($wp->matched_query); if ( !empty ( $args['paged'] ) && 0 == $paged ) { $wp_query->set('paged', $args['paged']); $paged = $args['paged']; } }
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
post-template.php
. But then I realized this:If I type, for example, the URL "(...)/blog/page/456", the blog template loads with the message "Nothing here". No 404 error page.
On the other hand, the URL "works/page/2", which I expect to return some posts, ends on a 404 error page.
Why?
EDIT:
On the 404 page I also get this error:
Trying to get property of non-object in (…)/wordpress/wp-includes/post-template.php on the line 29.
...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.
EDIT:
This is what I got by debugging the URL mysite.com/trabalhos/pagina/2 with the Debug-this plug-in. It seems to be very wrong:
Obs: trabalhos = works, pagina = page
Matched Rule: trabalhos/([^/]+)(/[0-9]+)?/?$
Matched Query: works=pagina&page=%2F2
Query String: page=%2F2&name=pagina&post_type=works&works=pagina&debug-this=rewrites
In my understanding, the URL mysite.com/trabalhos/pagina/2 SHOULD point to the following URL (except it doesn't work - but If i replace 'trabalhos' with 'noticias' - my slug for 'blog' - it works):
mysite.com/trabalhos/pagina/2/?pagename=trabalhos
However, the URL mysite.com/trabalhos/pagina/2 points to this weird url:
mysite.com/trabalhos/pagina/2?page=%2F2&works=pagina&post_type=works&name=pagina
And surprisingly, the following URL points exactly to the content I was expecting, that is, page 2 of works (trabalhos):
mysite.com/noticias/pagina/2/?pagename=trabalhos
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.
Is it some misconfiguration? Where would it be?
Thanks in advance for any help!
-
zok about 10 yearsTurns out it was indeed a CPT/page slug conflict. I used the same slug in my custom post type 'rewrite' property and on the page which was listing all posts from that custom post type, and apparently that completely messed up the WordPress query string.
-
zok about 10 yearsThis post also gave me good insights on the subject: code.tutsplus.com/tutorials/…
-
Adewale George almost 8 yearsYou saved me! 4hours wasted on this
-
Karls almost 2 yearsIf you use CPT UI, edit your custom post type and change the option Rewrite "(default: true) Whether or not WordPress should use rewrites for this post type" to false.