Title: Custom archive-$posttype.php without pre queried $posts
Last modified: August 31, 2016

---

# Custom archive-$posttype.php without pre queried $posts

 *  Resolved [pleinx](https://wordpress.org/support/users/pleinx/)
 * (@pleinx)
 * [10 years ago](https://wordpress.org/support/topic/custom-archive-posttypephp-without-pre-queried-posts/)
 * Hey guys,
 * i have a custom archive template called “archive-computer.php”.
    In this PHP-
   File we do execute custom queries only!
 * Well, i see now that wordpress already load entries from this post-type in $posts
   variable.
 * My Testfile “archive-computer.php”
 *     ```
       <?php
       /**
        * The archive template file
        *
        * @link http://codex.wordpress.org/Template_Hierarchy
        * @package WordPress
        * @subpackage mydomain.de
        */
             var_dump($posts); // i want to safe this query which fill this variable
   
             // here we do custom queries
       ?>
       ```
   
 * I want to safe the query which fill the $posts variable, is that possible?
    And
   no, i dont want only to _unset($posts);_ 🙂
 * thanks in advance

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

 *  [Tyler Shadick](https://wordpress.org/support/users/tyler-shadick/)
 * (@tyler-shadick)
 * [10 years ago](https://wordpress.org/support/topic/custom-archive-posttypephp-without-pre-queried-posts/#post-7431559)
 * Hey pleinx,
 * This article may help guide you in building your archive template: [https://www.smashingmagazine.com/2015/04/building-custom-wordpress-archive-page/](https://www.smashingmagazine.com/2015/04/building-custom-wordpress-archive-page/)
 * As for the variable, you should just be able to save the variable using another
   variable, like so:
 *     ```
       $saved_posts = $posts;
       ```
   
 * You should then be able to use that variable again somewhere else in the code
   by using $saved_posts. As for building the archive page, I’d suggest reviewing
   the above article.
 * Hope this helps!
    -Tyler
 *  Thread Starter [pleinx](https://wordpress.org/support/users/pleinx/)
 * (@pleinx)
 * [10 years ago](https://wordpress.org/support/topic/custom-archive-posttypephp-without-pre-queried-posts/#post-7431564)
 * Hi Tyler,
 * thanks for your reply but i know the handling of building custom templates. Thats
   not the problem.
 * I want to save the (mySQL-)QUERY not the $posts array with post-objects!
    We 
   dont need the default loading posts here. We fire custom queries.
 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [10 years ago](https://wordpress.org/support/topic/custom-archive-posttypephp-without-pre-queried-posts/#post-7431565)
 * when you say
 * > I want to save the (mySQL-)QUERY not the $posts array with post-objects!
   >  We
   > dont need the default loading posts here. We fire custom queries.
 * do you mean you do not want the archive template to run a query for that post
   type? That’s the entire purpose of archive-cpt.php
 *  Thread Starter [pleinx](https://wordpress.org/support/users/pleinx/)
 * (@pleinx)
 * [10 years ago](https://wordpress.org/support/topic/custom-archive-posttypephp-without-pre-queried-posts/#post-7431567)
 * [@sterndata](https://wordpress.org/support/users/sterndata/)
    yes. That would
   be nice!
 * And yes too, i know but the customer want it 😉 (file-structure, maybe for easy
   rewriting URLs)
 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [10 years ago](https://wordpress.org/support/topic/custom-archive-posttypephp-without-pre-queried-posts/#post-7431568)
 * Please explain what you’re trying to accomplish.
 *  Thread Starter [pleinx](https://wordpress.org/support/users/pleinx/)
 * (@pleinx)
 * [10 years ago](https://wordpress.org/support/topic/custom-archive-posttypephp-without-pre-queried-posts/#post-7431570)
 * What more can I explain when I say I want to save a request? (No offense!)
 * I am concerned also also to the possibility. A dream would be if it were realized
   with hooks.
 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [10 years ago](https://wordpress.org/support/topic/custom-archive-posttypephp-without-pre-queried-posts/#post-7431571)
 * well, I think tyler anserwed it, then. stick $posts into another variable.
 *  Thread Starter [pleinx](https://wordpress.org/support/users/pleinx/)
 * (@pleinx)
 * [10 years ago](https://wordpress.org/support/topic/custom-archive-posttypephp-without-pre-queried-posts/#post-7431579)
 * hehe, but then i have nothing saved (query). I clone only variables!
    I want 
   to save the mysql-query which get the posts from database guys so that $posts
   is undefined or empty. And no i doesnt want to set $posts = null 😉
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [10 years ago](https://wordpress.org/support/topic/custom-archive-posttypephp-without-pre-queried-posts/#post-7431587)
 * It’s not really clear what you want to do 🙂
 * Why the need to change (or save) the original query? Maybe a better option is
   to change the query in the ‘pre_get_posts’ action hook.
    [https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts](https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts)
 * If you want to change the query so it returns no posts in the computer post type
   archive use this in your (child) theme’s functions.php file
 *     ```
       add_filter( 'posts_where', 'invalidate_computer_archive_query', 10, 2 );
   
       function invalidate_computer_archive_query( $where, $query ) {
       	if ( !is_admin() && $query->is_main_query() && is_post_type_archive( 'computer' ) ) {
       		// invalidate query for the computer custom post type
       		$where .= " AND 1 = 0";
       	}
   
       	return $where;
       }
       ```
   
 * btw:
    consider creating a [child theme](http://codex.wordpress.org/Child_Themes)
   instead of editing your theme directly – if you upgrade the theme all your modifications
   will be lost. Or [create a plugin](https://wordpress.org/plugins/pluginception/)
   with the code above.
 *  Thread Starter [pleinx](https://wordpress.org/support/users/pleinx/)
 * (@pleinx)
 * [10 years ago](https://wordpress.org/support/topic/custom-archive-posttypephp-without-pre-queried-posts/#post-7431669)
 * I dont know what is so hard to understand on “i want to save a query” 🙂
    Its
   so simple. WordPress fire a default query on each archive-$posttype.php. But 
   we dont need the default postings for this posttype here. We use custom queries
   from other database and performance is still important, so, i want to save this
   query-execute-time.
 * i found my perfect solution here:
    vadimk.com/blog/disable-wordpress-search-query/
 * thats easy and clear with small modifications like your example [@keesiemeijer](https://wordpress.org/support/users/keesiemeijer/).
   thanks for it!
 *     ```
       function _cancel_query( $query ) {
   
           if ( !is_admin() && is_post_type_archive( 'computer' ) ) {
               $query = false;
           }
   
           return $query;
       }
   
       add_action( 'posts_request', '_cancel_query' );
       ```
   
 * but thanks for your help! 🙂
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [10 years ago](https://wordpress.org/support/topic/custom-archive-posttypephp-without-pre-queried-posts/#post-7431687)
 * You’re welcome 🙂
 * > I dont know what is so hard to understand on “i want to save a query” 🙂
 * It’s the word ‘save’ that was confusing. The solution you chose is not ‘saving’
   a query, it’s ‘changing’ the query so it’s not run or returns empty. My initial
   thought was you wanted to store (save) the query somewhere, be it in a variable
   or the database.
 * I’m glad you have it resolved

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

The topic ‘Custom archive-$posttype.php without pre queried $posts’ is closed to
new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 11 replies
 * 4 participants
 * Last reply from: [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * Last activity: [10 years ago](https://wordpress.org/support/topic/custom-archive-posttypephp-without-pre-queried-posts/#post-7431687)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
