Make WordPress Core

Opened 15 years ago

Closed 15 years ago

#11067 closed defect (bug) (wontfix)

Using query_posts() on any subcategories the attached posts_per_page attribute gets lost

Reported by: voetzi's profile voetzi Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.8.5
Component: General Keywords:
Focuses: Cc:

Description

I have a subcategory containing 9 posts. If this category is showed with the archive.php temlate, i want it to show only one post per page. The default posts per page value in the WP settings is 5. So i build a query this way:

query_posts( $query_string . '&posts_per_page=1' );

This works fine for:

http://domain.tld/category/cat/subcat/page/1 and http://domain.tld/category/cat/subcat/page/2

Remember 9 posts in that subcategory and a default posts per page value of 5. If I point my browser to:

http://domain.tld/category/cat/subcat/page/3

I get a "not found page" (not a 404) with the search form on it. So i added this code to the searchform.php:

<?php echo"Query:". urldecode($query_string;)?>

The result is: paged=3&category_name=cat/subcat The posts_per_page is missing.

After figured out this, I changed the posts per page in the WP settings to 1 and it worked, as it should. Changing it to 3 it works till page 3 and i have the same effect on page 4.

This is why it seems to me, that the posts_per_page attributes gets lost during processing and then is overwritten with the default value.

Change History (1)

#1 @dd32
15 years ago

  • Resolution set to wontfix
  • Status changed from new to closed

This would be occuring as WordPress queries posts & paginates & subsequently 404's if there are not posts are available.

After all that happens, If there are posts available, THEN it reads your template, and then repeats everything again thanks to query_posts.

Solution? Don't use query_posts(), Instead, add a hook and set the 'posts_per_archive_page' query var, The advantage of this, is that it acually works, and you dont query the Database multiple times for every archive load (Which is what search engines will be hitting).

Code to "fix" it for you: (In your themes functions.php file)

add_filter('request', '_request_1_per_archive');
function _request_1_per_archive($vars) {
	$vars['posts_per_archive_page'] = 1;
	return $vars;
}

Theres no need for if(is_archive()) or anything, WP_Query will only use that var if its actually a archive page. For the code where its used, see line 1608 of wp-includes/query.php

Note: This doesn't affect feeds.

Closing as wonfix, due to this not being something that can be supported within templates.

Note: See TracTickets for help on using tickets.