Title: Query posts in archive.php
Last modified: August 21, 2016

---

# Query posts in archive.php

 *  [mageguild](https://wordpress.org/support/users/mageguild/)
 * (@mageguild)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/query-posts-in-archivephp/)
 * I am trying to run an extra query within the archive.php. But everytime time 
   it executes, it returns the same results which are not the results that I am 
   after.
 * Below is a copy of my code that I am running in the archive file, which is located
   before the if (have_posts()) : while (have_posts()) : the_post(); code.
 * The code should get the slug of the tag that I am viewing, and use that to check
   if there is another post in a different post type with the same slug.
 *     ```
       $tag       = get_queried_object();
       $tag_title = $tag->name; // Same as single_tag_title()
       $tag_slug  = $tag->slug;
       $tag_id    = $tag->term_id;
       $CheckForPost = array(
       	'post_type' => 'player-profiles',
       	'post_status' => 'publish',
       	'posts_per_page' => 1,
       	'post_slug' => '$tag_slug'
       );
       $GetProfileCheck = null;
       $GetProfileCheck = new WP_Query($CheckForPost);
       if (have_posts()){
       	// return true
       }else{
       	// return false
       }
       ```
   

Viewing 1 replies (of 1 total)

 *  [Andrew Bartel](https://wordpress.org/support/users/andrew-bartel/)
 * (@andrew-bartel)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/query-posts-in-archivephp/#post-3651269)
 * have_posts() is a template tag, meaning, it will use the current loop/query. 
   WP_Query is an object (class) to create a _secondary_ loop. Your example:
 *     ```
       $GetProfileCheck = null; // fyi this is unnecessary
       $GetProfileCheck = new WP_Query($CheckForPost);
       if ($GetProfileCheck->have_posts()){
       	// return true
       }else{
       	// return false
       }
       ```
   
 * I commend you for not copping out and using query_posts() btw.

Viewing 1 replies (of 1 total)

The topic ‘Query posts in archive.php’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [Andrew Bartel](https://wordpress.org/support/users/andrew-bartel/)
 * Last activity: [13 years, 1 month ago](https://wordpress.org/support/topic/query-posts-in-archivephp/#post-3651269)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
