Title: Conditional WP_Query with PHP
Last modified: August 24, 2016

---

# Conditional WP_Query with PHP

 *  [finnlesueur](https://wordpress.org/support/users/finnlesueur/)
 * (@finnlesueur)
 * [11 years ago](https://wordpress.org/support/topic/conditional-wp_query-with-php/)
 * Hey,
 * I am trying to conditionally perform a WP_Query depending on what page I am on.
   This is necessary because I want to pull a series of tags depending on what page
   I am on.
 * eg. The ‘Politics and Debates’ page will have posts from both the tags ‘Politics’
   and ‘Debates’. My thoughts are to use this code in my page.php file:
 *     ```
       $page = get_the_title( $page_id );
           echo $page;
           if ($page = 'Gender & Equality') {
               $tag_query = new WP_Query ( 'tag=Gender,Equality,Sexuality' );
           } elseif ($page ='Science & Research') {
               $tag_query = new WP_Query ( 'tag=Science,Research' );
           } elseif ($page ='Politics & Debates') {
               $tag_query = new WP_Query ( 'tag=Politics,Debates' );
           } elseif ($page ='Science & Environment') {
               $tag_query = new WP_Query ( 'tag=Science,Environment' );
           } elseif ($page ='Scientific Research') {
               $tag_query = new WP_Query ( 'tag=Research' );
           } elseif ($page ='Local Events') {
               $tag_query = new WP_Query ( 'tag=Events' );
           } else {
               echo 'No posts found';
           }
   
           while ( $tag_query -> have_posts() ) {
               $tag_query -> the_post();
               echo '<div class="masonry-item"><p>' . get_the_title() . '</p></div>';
           };
       ```
   
 * However, when I view any of my pages only posts from this query are loaded.
 *     ```
       $tag_query = new WP_Query ( 'tag=Gender,Equality,Sexuality' );
       ```
   
 * Any suggestions would be appreciated.

The topic ‘Conditional WP_Query with PHP’ is closed to new replies.

## Tags

 * [if statement](https://wordpress.org/support/topic-tag/if-statement/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [tags](https://wordpress.org/support/topic-tag/tags/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * 0 replies
 * 1 participant
 * Last reply from: [finnlesueur](https://wordpress.org/support/users/finnlesueur/)
 * Last activity: [11 years ago](https://wordpress.org/support/topic/conditional-wp_query-with-php/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
