[wp-testers] WP_Query rewrites $request.

Jamie Talbot wphackers at jamietalbot.com
Mon Jan 9 16:48:19 GMT 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

Trying to get a multiple 'The Loop' template working, I came across
something unexpected.  Following the instructions in the Codex[1], I
created a new WP_Query and used it for the subsection of posts that I
needed (a moblog category).

$keitai_display = new WP_Query('category_name=keitai&showposts=2');
while ($keitai_display->have_posts()) : $keitai_display->the_post();
<!-- Do some stuff -->
endwhile;

After doing stuff for that one category, I called The Loop as usual:

if (have_posts()) : while (have_posts()) : the_post();
<!-- Do some more stuff -->
endwhile;
post_nav_link();

The posts displayed as expected but the page numbering in post_nav_link
was screwed up, saying only 2 pages, instead of 18.  Saving $request
before calling new WP_Query and then replacing it afterwards fixed the
problem.

$saved = $request;
$keitai_display = new WP_Query('category_name=keitai&showposts=2');
while ($keitai_display->have_posts()) : $keitai_display->the_post();
<!-- Do some stuff -->
endwhile;
$request = $saved;
if (have_posts()) : while (have_posts()) : the_post();
<!-- Do some more stuff -->
endwhile;
post_nav_link();

$request is overwritten in WP_Query->get_posts() (Line 645 or
thereabouts).  Because the $request is built for $wp_query first, any
new WP_Query objects in a template will overwrite it.  Instead of
changing $request for each new WP_Query, could the query string be
assigned to a class member variable?  This way, each WP_Query would have
it's own request string, which would remain consistent.

I haven't submitted a ticket for this yet, because I don't know the
extent to which $request is used throughout the rest of the code (or if
other plugins rely on it being written this way).  Is it a bug?  Or just
the expected behaviour and an omission from the documentation?

Cheers,

Jamie

[1] http://codex.wordpress.org/The_Loop - Multiple Loops Example 3

- --
http://jamietalbot.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDwpPTrovxfShShFARAtOvAJ4tIEVwPahY7+SReNroNkLzQRzpLgCePZpV
OAeSVhEZd+xPNIsGjB45cEs=
=qYVe
-----END PGP SIGNATURE-----


More information about the wp-testers mailing list