[wp-hackers] wpdb and qp_query, and how to save query results for posts_nav_link

Matthias Tarasiewicz p.a.r.a.s.e.w at gmx.net
Thu Jul 21 22:16:00 GMT 2005


i have a question regarding multiple loops: as i am using several  
queries to show the last entries in various categories, but the  
posts_nav_link seems to lose the information about the previous  
entries. if i comment out the other queries, everything works fine.

i posted a code snipped to show, that the way to store the wp_query  
object as described in http://codex.wordpress.org/the_loop does not  
work correctly. i found out that the $wp_query changes, even though  
i'm trying to write it back. any ideas how to save the original  
query, so the posts_nav_link works on the search pages?

the following code i am putting in the header.php to get the  
following outut
before:39 after:23
before and after should be the same. any ideas?


<?php

global $request, $posts_per_page, $wpdb, $max_num_pages;
$temp_query = $wp_query;
$wpdb_bkp=$wpdb;

preg_match('#FROM (.*) GROUP BY#', $request, $matches);
$fromwhere = $matches[1];
echo "before:".$wpdb->get_var("SELECT COUNT(ID) FROM $fromwhere");

# saving the posts_per_page and wpdb
$posts_per_page_backup = $posts_per_page;
$wpdb_backup = $wpdb;


$my_query=new WP_Query('category_name=Events&showposts=1');
if ($my_query->have_posts()) : while ($my_query->have_posts()) :  
$my_query->the_post();
$link_events=$post->ID;
endwhile; endif;

# writing back posts_per_page and wpdb
$wpdb=$wpdb_bkp;
$posts_per_page = $posts_per_page_backup;
$wpdb = $wpdb_backup;

$wp_query = $temp_query;

echo(" after:");
preg_match('#FROM (.*) GROUP BY#', $request, $matches);
$fromwhere = $matches[1];
echo $wpdb->get_var("SELECT COUNT(ID) FROM $fromwhere");

?>


More information about the wp-hackers mailing list