Title: Terminating PHP?
Last modified: August 20, 2016

---

# Terminating PHP?

 *  [Chris H](https://wordpress.org/support/users/chris-h/)
 * (@chris-h)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/terminating-php/)
 * I’m using the following code to display a sinlge upcoming event at the very top
   of my websites home page, above the “blog” that will display all of the posts
   from another category.
 *     ```
       <div id="home-nextevent">
   
       <?php query_posts('category_name=events&showposts=1&meta_key=sortdate&orderby=meta_value&order=asc'); ?>
   
       <?php while (have_posts()) : the_post(); ?>
   
       <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
   
       <?php endwhile; ?>
   
       </div> <!-- closes "home-nextevent" -->
       ```
   
 * However, this code appears to affect the remainder of the website, causing my
   blog section (still on the home/index.php page) to only display a single post.
 * I’m a PHP beginner, and I’m wondering if the event code needs to be terminated
   somehow, so that it doesn’t mess the rest of the page up. Is there an easy fix
   for this?

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/terminating-php/#post-2406582)
 * Each call to query_posts() destroys the previous one. One call is made before
   your template is loaded, and your call overwrites it. So, the second loop just
   repeats the first one.
 * You must either save and restore the query, or use WP_Query() instead of query_posts().
   Here is how you would save the query:
 *     ```
       <?php $temp_query = $wp_query; ?>
       <div id="home-nextevent">
   
       <?php query_posts('category_name=events&showposts=1&meta_key=sortdate&orderby=meta_value&order=asc'); ?>
   
       <?php while (have_posts()) : the_post(); ?>
   
       <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
   
       <?php endwhile; ?>
   
       </div> <!-- closes "home-nextevent" -->
       <?php $wp_query = $temp_query; ?>
       ```
   
 *  Thread Starter [Chris H](https://wordpress.org/support/users/chris-h/)
 * (@chris-h)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/terminating-php/#post-2406701)
 * Hi there,
 * Thanks so much for your reply. I tried putting the code you posted into my template,
   but couldn’t get anything to show up. Is there something I’m still not doing 
   right, do you think?
 * Thanks again.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/terminating-php/#post-2406702)
 * Almost impossible to say without seeing the code. Please put your code into a
   [pastebin](http://pastebin.com) and post a link to it here.
 *  Thread Starter [Chris H](https://wordpress.org/support/users/chris-h/)
 * (@chris-h)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/terminating-php/#post-2406703)
 * Thanks for the tip on Pastebin. I’ve never used that before. 🙂
 * [http://pastebin.com/iWJiHemU](http://pastebin.com/iWJiHemU)
 * Cheers,
 * Chris
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/terminating-php/#post-2406704)
 * The code in question is not in the pastebin.
 * Does the code you posted at first show anything? I thought that it showed the
   correct post, but messed up the display of the following posts. If that is not
   the case, get that code working and put it between the save and restore:
 *     ```
       <?php $temp_query = $wp_query; ?>
   
          <!-- Your working code here -->
   
       <?php $wp_query = $temp_query; ?>
       ```
   
 *  Thread Starter [Chris H](https://wordpress.org/support/users/chris-h/)
 * (@chris-h)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/terminating-php/#post-2406706)
 * Seems to be working now. Not sure what I’ve done differently, as I simply copied
   and pasted the code both times. Bizarre. 🙂
 * Thanks so much for your help on this. I really appreciate it.
 * Cheers,
 * Chris

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Terminating PHP?’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 2 participants
 * Last reply from: [Chris H](https://wordpress.org/support/users/chris-h/)
 * Last activity: [14 years, 7 months ago](https://wordpress.org/support/topic/terminating-php/#post-2406706)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
