Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

WP_Query and pagination error WordPress

  • SOLVED

Take the following code for author.php:


<?php

/* GET STUFF */

$director_id = get_query_var('author');
$reel = get_query_var('reel');
$info = get_query_var('info');
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;



$director_args = array(
'meta_key' => '_exit_info_metabox_director',
'meta_value' => $director_id,
'paged' => $page,
'types' => $reel, // CPT taxonomy
'posts_per_page' => 2,
'post_type' => 'clip',
'orderby' => 'menu_order',
'order' => 'ASC',
);

//Fancy pagination fixery
$temp = $wp_query;
$wp_query = null;

//Query instance
$wp_query = new WP_Query();
$wp_query->query($director_args);
?>

<?php if ($wp_query->have_posts()) : ?>

<?php next_posts_link('<span class="nextpost">Next</span>',$wp_query->max_num_pages ); ?>
<?php previous_posts_link('<span class="previouspost">Back</span>' ); ?>

<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

<!-- DO STUFF -->

<?php endwhile; ?>

<?php $wp_query = null; $wp_query = $temp;?>


<?php endif; ?>


<?php

//Reset query so sidebar can use is_author
wp_reset_query();

?>


This code takes an author id (Director) and retrieves a corresponding list of clips by that author. Note that the Director is not necessarily the author of a post.

Everything works fine up to a point. The author.php file is successfully invoked using a URL like /?author=1&reel=commercials. However if we try to retrieve the paged content (which does exist) using a url like /?author=1&reel=commercials&paged=2 we get a 404 error.

I am using default permalinks and query vars are registered using:

add_filter('query_vars', 'exit_query_vars');

function exit_query_vars($public_query_vars) {

$public_query_vars[] = 'member';
$public_query_vars[] = 'types';
$public_query_vars[] = 'info';
$public_query_vars[] = 'reel';
$public_query_vars[] = 'news-item';

return $public_query_vars;

}


What is causing this 404 error and how do I fix it?

Answers (5)

2011-11-04

Peter Michael answers:

Move <?php $wp_query = null; $wp_query = $temp;?> below the <?php endif; ?> and try again.


Peter Michael comments:

Forget my last answer. If you change the var to '$paged' instead of '$page' does it work?


Niels comments:

I doesn't I'm afraid. The paged var in the URL is generated by the <strong>next_posts_link</strong> and <strong>previous_posts_link</strong> functions. Also changing the url from <strong>&paged=2</strong> to <strong>&page=2</strong> has the same result.


Niels comments:

Actually, not the same result. It just doesn't advance to the next posts but shows the first page of posts ( I assume because the var is being ignored).


Peter Michael comments:

Change the $page var in your code to $paged, the functions next_posts_link and previous_posts_link access that via global $paged.

2011-11-04

Pau answers:

try this: replace the code below

$director_id = get_query_var('author');

$reel = get_query_var('reel');

$info = get_query_var('info');

$page = (get_query_var('paged')) ? get_query_var('paged') : 1;


with:

global $paged;

if( is_front_page() ){ $paged = (get_query_var('page')) ? get_query_var('page') : 1; }

$director_id = get_query_var('author');

$reel = get_query_var('reel');

$info = get_query_var('info');


then replace this code:

'paged' => $page,

with:

'paged' => $paged,


Niels comments:

This did not work I'm afraid. I'm suspect that the author.php template is the issue.

2011-11-04

Daniele Raimondi answers:

I think wp is bugged here and I'm wondering why this problem has never been patched yet. I've wrote a short "story" about the 404/pagination problem on wp forum. It describes what I think is going on in wp (based on my debugging) when you change the number of item per page "on the fly" and tryes to give a workaround that is working perfectly right now with all my up-to-date wp installations.

Hope it can be usefull:
[[LINK href="http://wordpress.org/support/topic/explanation-and-workaround-for-error-404-on-category-pagination?replies=1"]]
http://wordpress.org/support/topic/explanation-and-workaround-for-error-404-on-category-pagination?replies=1[[/LINK]]


Niels comments:

I have also found that setting the post per page value in backend to partially correct this problem. I am going to try this approach. Hardcoding the taxonomies is not really a good long-term solution though.


Daniele Raimondi comments:

But you are already hardcoding a taxonomy name ('reel') and a value for 'post per page': infact you have coded them in your template file so this is basically the same thing.

My approach, apart from resolving the 404 problem, centralizes in one place all your 'post per page' values. And if you don't specify a custom value for a taxonomy, it takes the default value setted in the backend.
So no more 404 errors and only one piece of code to update.

The complete solution would be using my code to create a plugin with a settings page, where all your taxonomies are listed and where you can set a 'post per page' for each taxonomy (if different from default).

In your particular case you can also set the default value to the minimum (i.e. 1) in the backend. As setting a greater value than the default one doesn't leed to a 404 error, this can resolve your problem but you have to readjust the 'post per page' values in all of your template files.


Niels comments:

Ah, of course you're right about the hardcoding. My apologies :-)

I tried this solution with good results, except some other queries were then affected.

The solution I used in the end actually involved creating a new query var "directors" and using a conditional on the index page to see if that var is being queried, and loading a special loop for that condition using query_posts() instead. It works well.

However, I do thank you for your work!

2011-11-04

Luis Abarca answers:

I think the tax_query should be this way, i dont know if its neccesary to use $wp_query, you can use another var name like clips.

Changes on code are bold.


<?php
/* GET STUFF */
$director_id = get_query_var('author');
$reel = get_query_var('reel');
$info = get_query_var('info');
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;


$director_args = array(
'meta_key' => '_exit_info_metabox_director',
'meta_value' => $director_id,
'paged' => $page,
<strong>
'tax_query' => array( // CPT taxonomy
array(
'taxonomy' => 'reel',
'field' => 'slug',
'terms' => $reel
)
),
</strong>
'posts_per_page' => 2,
'post_type' => 'clip',
'orderby' => 'menu_order',
'order' => 'ASC',
);

//Fancy pagination fixery
//Query instance
<strong>$clips = new WP_Query($director_args);</strong>
?>

<?php if ($clips->have_posts()) : ?>
<?php next_posts_link('<span class="nextpost">Next</span>', $clips->max_num_pages ); ?>
<?php previous_posts_link('<span class="previouspost">Back</span>' ); ?>

<?php while ($clips->have_posts()) : $clips->the_post(); ?>
<!-- DO STUFF -->
<?php endwhile; ?>
<?php $clips = null; ?>
<?php endif; ?>

<?php
//Reset query so sidebar can use is_author
wp_reset_query();
?>

2011-11-06

Darrin Boutote answers:

$page and $paged are global vars used by WP for pagination purposes. WP should know which page to pull on its own without needing $page = (get_query_var('paged')) ? get_query_var('paged') : 1; set. Try commenting out that line and change 'paged' => $page, to 'paged' => $paged, in your args array.

You can also check out this article I wrote a while back on the subject: http://darrinb.com/notes/2009/how-to-paginate-a-custom-wordpress-query-loop/3/