Storing a Query
This post is part of a series (Wordpress Theme Tips):
- Storing a Query
- Ajax Usability: Tab History
- Semantic Details
- Fine Tuned 404
Note: This post is over a year old. You may want to check later in this blog to see if there is new information.
The following links are auto-generated but may help you locate newer content:
I’ve finally “finished” the C6 website to my satisfaction and am ready to start blogging a little more regularly. I thought I’d kick it off with a new series on “how I did it” and reveal a few techniques I learned/developed in the process of making the new site. Today I’m going to go over the last one I figured out: Storing a Wordpress database query while you run a second one on a page…
There are a couple of ways to go about this, I’m going to cover the one that worked for me. It involves cloning the query into a temp variable and then running query_posts again. In the code below you can see what I’m talking about:
The single.php page
[The requested file http://blog.circlesixdesign.com/wp-content/uploads/single.txt could not be found]
This is the single.php page. It starts out with a single post in the wp_query variable, and I wanted to get a list of all the other posts in the same category for the purposes of navigation. So we run the first part of the loop to generate some variables and some text for the sidebar:
First Loop
< ?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1 id="pagetitle">< ?php the_title(); ?></h1>
<div id="secondary">
<h3>Viewing</h3>
<p class="sidetext">
< ?php $theClient = get_post_custom_values('client'); ?>
< ?php if (in_category(7)) { $category = "Web Design"; $cat = '7';}?>
< ?php if (in_category(8)) { $category = "Print Design"; $cat = '8';} ?>
< ?php if (in_category(9)) { $category = "Audio/Video Design"; $cat = '9';} ?>
You are currently viewing < ?php the_title(); ?> in the < ?php echo $category; ?> category, a piece done by Circle Six Design for < ?php echo $theClient[0]; ?>.
</p>
< ?php endwhile; endif; ?>
</div>
Then we store the single post query as a temporary variable that we can come back to in a second. Between the statements that copy and restore the query, we run a second loop. The “clone” part of the command is for PHP 5, if you’re using 4, then you’ll want to remove the “clone” from the statement:
Storing the query and running another loop
< ?php $temp_query = clone $wp_query; ?>
< ?php query_posts('cat='.$cat.'&showposts=100'); ?>
<h4>Other Pieces in < ?php echo $category ?></h4>
<ul>
< ?php while (have_posts()) : the_post(); ?>
<li></li>
< ?php endwhile; ?>
<li>« Back to the Work Page</li>
</ul>
< ?php $wp_query = clone $temp_query; ?>
After restoring wp_query, we start up the loop again and finish out the page:
Returing to the original loop
< ?php if (have_posts()) : while (have_posts()) : the_post(); ?>
< ?php edit_post_link('Edit this entry.', '<p>', ''); ?>
<div id="primary">
<h2>< ?php the_title(); ?></h2>
<div class="post">
< ?php the_content('<p class="serif">Read the rest of this entry »'); ?>
</div>
< ?php endwhile; else: ?>
<h2>Post not found...</h2>
<p>Sorry, no posts matched your criteria.</p>
< ?php endif; ?>
</div>
And that’s how I made the pages that show the individual portfolio pieces. To see it in action, just view any piece in the Work section of the new Circle Six site. 
Comments are closed
Comments are currently closed on this entry.