Title: Custom query before default query in single-posttype.php
Last modified: August 20, 2016

---

# Custom query before default query in single-posttype.php

 *  Resolved [OrT](https://wordpress.org/support/users/ort/)
 * (@ort)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/custom-query-before-default-query-in-single-posttypephp/)
 * Beloved WordPressers,
 * In single-posttype.php, I’m doing a custom query before the regular `<?php if(
   have_posts()) : while (have_posts()) : the_post(); ?>`.
 * _[please mark any code in your post – see [http://codex.wordpress.org/Forum\_Welcome#Posting\_Code](http://codex.wordpress.org/Forum_Welcome#Posting_Code)]_
 * Before the custom query, I clone the wp_query:
 *     ```
       $temp =  clone $wp_query;
       ```
   
 * After the custom query, I set it back and erase postdata:
 *     ```
       <?php $my_query = null; $my_query = clone $temp; ?>
       <?php wp_reset_postdata(); ?>
       ```
   
 * But the detail post that should be showing shows all the posts that the first
   query (custom query) finds.
 * What am I doing wrong?
    Thanks a lot for your time, much obliged!

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

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/custom-query-before-default-query-in-single-posttypephp/#post-2704996)
 * are you using `$my_query` for the secondary loop?
 * possibly just a typing mistake; and maybe rearrange the code:
 *     ```
       <?php wp_reset_postdata(); ?>
       <?php $my_query = null; $wp_query = clone $temp; ?>
       ```
   
 *  [Chip Bennett](https://wordpress.org/support/users/chipbennett/)
 * (@chipbennett)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/custom-query-before-default-query-in-single-posttypephp/#post-2704997)
 * How are you running your custom query, via `new WP_Query()` (_correct_), or via`
   query_posts()` (_incorrect_)?
 * If you’re using `query_posts()`:
 * 1. Don’t do that. That function is intended _only_ to modify the primary loop
   query.
    2. Use `WP_Query()` instead. 3. Then, `wp_reset_postdata()` will work
   as expected
 *  Thread Starter [OrT](https://wordpress.org/support/users/ort/)
 * (@ort)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/custom-query-before-default-query-in-single-posttypephp/#post-2704998)
 * Ok, that was very stupid of me. After hours of searching and replacing.
    Thank
   you for that, alchymyth 🙂
 *  Thread Starter [OrT](https://wordpress.org/support/users/ort/)
 * (@ort)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/custom-query-before-default-query-in-single-posttypephp/#post-2704999)
 * Chip – not that it matters now, but I was running WP_Query().
    Thanks for your
   time, though!
 *  [Chip Bennett](https://wordpress.org/support/users/chipbennett/)
 * (@chipbennett)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/custom-query-before-default-query-in-single-posttypephp/#post-2705003)
 * > not that it matters now, but I was running WP_Query()
 * Music to my ears! 🙂

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

The topic ‘Custom query before default query in single-posttype.php’ is closed to
new replies.

## Tags

 * [custom-query](https://wordpress.org/support/topic-tag/custom-query/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)

 * 5 replies
 * 3 participants
 * Last reply from: [Chip Bennett](https://wordpress.org/support/users/chipbennett/)
 * Last activity: [14 years, 1 month ago](https://wordpress.org/support/topic/custom-query-before-default-query-in-single-posttypephp/#post-2705003)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
