Title: Combine two wp_Querys?
Last modified: August 22, 2016

---

# Combine two wp_Querys?

 *  [jomurgel](https://wordpress.org/support/users/jomurgel/)
 * (@jomurgel)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/combine-two-wp_querys/)
 * Hello. I’m generally a n00b at strings of php, but I’ve built/tweaked a little
   bit of code here built to pull a list of posts from a custom post type while 
   simultaneously highlighting the current post title if you’re on that page. The
   query works, but in order to get it to work I have two $query = new WP_Querys
   going on, and I’m trying to combined them, just to make everything a little cleaner.
 *     ```
       $query = new WP_Query('category_name=' . $category->locations);
       // Workaround for Cateogry Variable
       $query = new WP_Query( array( 'post_type' => 'locations', 'orderby' => 'name', 'order' => 'ASC') );
       ```
   
 * Any suggestions? (FULL code below)
 *     ```
       <ul>
       	<?php
       	$IDOutsideLoop = $post->ID;
       	while( have_posts() ) {
       		the_post();
       		foreach( ( get_the_category() ) as $category )
       			$query = new WP_Query('category_name=' . $category->locations);
       			// Workaround for Cateogry Variable
       			$query = new WP_Query( array( 'post_type' => 'locations', 'orderby' => 'name', 'order' => 'ASC') );
       		if( $query ) {
       			while ( $query->have_posts() ) {
       				$query->the_post(); ?>
       				<li<?php print ( is_single() && $IDOutsideLoop == $post->ID ) ? ' class="current-locale"' : ''; ?> id="post-<?php the_ID(); ?>" <?php post_class(); ?> >
       					<a>"><?php the_title(); ?></a>
   
       	<?php
       			}
       		}
       	} wp_reset_postdata();
       	?>
       </ul>
       ```
   
 * Thanks in advance!

The topic ‘Combine two wp_Querys?’ is closed to new replies.

## Tags

 * [combined](https://wordpress.org/support/topic-tag/combined/)
 * [custom post type](https://wordpress.org/support/topic-tag/custom-post-type/)
 * [post](https://wordpress.org/support/topic-tag/post/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [jomurgel](https://wordpress.org/support/users/jomurgel/)
 * Last activity: [11 years, 7 months ago](https://wordpress.org/support/topic/combine-two-wp_querys/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
