WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#32409 closed defect (bug) (invalid)

paginate_links() shows 404 on custom post type archive

Reported by: Kuzmanov Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.3
Component: Query Keywords:
Focuses: Cc:

Description

This is happening only on custom post type archive pages.

The scenario is this:

  • If I remove the posts_per_page argument from WP_Query it reads the value from Settings > Reading and works fine.
  • If the value of posts_per_page in WP_Query is bigger or equal than the value in Settings > Reading it works fine.
  • The problem: If the value of posts_per_page in WP_Query is smaller than the value in Settings > Reading it shows page not found on every /page/x.
Settings WP_Query
OK 5 /
OK 5 5 or 5+
NOT OK 5 4 or smaller

Here is my query:

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$wp_query = new WP_Query( array(
        'post_type' => 'custom_post_type_name',
        'post_status' => 'publish',
	'paged' => $paged,
	'posts_per_page' => 5,
) );

And here is the pagination:

global $wp_query;
$total = $wp_query->max_num_pages;

if ( $total > 1 )  {
	// get the current page
	if ( !$current_page = get_query_var('paged') ) {
		$current_page = 1;
	}

	echo paginate_links(array(
		'current'  => $current_page,
		'format'    => '/page/%#%',
		'total'    => $total,
		'mid_size' => 4,
		'type'     => 'list',
		'next_text' => 'next posts',
		'prev_text' => 'prev posts'
	));
}

Change History (4)

#1 follow-up: @boonebgorges
4 years ago

I've spent some time trying to reproduce this, given the info you've provided here. I'm able to reproduce some weird behavior, but I don't think this is the fault of paginate_links(). I think the problem has something to do with the way that you are overriding the $wp_query global - it's likely that you're doing it after the main query has already run (see WP::main() to get a sense of the load order).

To put the same point another way: When your posts_per_page is greater than 5, /page/3/ works. When posts_per_page is less than 5, the same URL, /page/3, does not work. This has nothing to do with paginate_links().

Can you provide more complete details about exactly how you are overloading the query? Does your first snippet ($wp_query = ...) appear in a theme template file?

#2 @SergeyBiryukov
4 years ago

  • Component changed from General to Query

If you're overriding the main query, I'd suggest using pre_get_posts action instead of a custom WP_Query instance.

See also some related tickets in 13:ticket:20758.

#3 in reply to: ↑ 1 @Kuzmanov
4 years ago

Replying to boonebgorges:

Can you provide more complete details about exactly how you are overloading the query? Does your first snippet ($wp_query = ...) appear in a theme template file?

This is my whole $wp_query code and yes, it's the first query that appears in the template file (custom-post-type-archive.php).

Last edited 4 years ago by Kuzmanov (previous) (diff)

#4 @boonebgorges
4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Thanks for the details, Kuzmanov. Based on this, I'd say that the problem is in fact unrelated to paginate_links(), and is caused by overriding the $wp_query global. If you need to modify params like 'posts_per_page', I'd recommend using the 'pre_get_posts' action, as suggested by SergeyBiryukov.

Note: See TracTickets for help on using tickets.