Title: Querying posts based on category
Last modified: August 20, 2016

---

# Querying posts based on category

 *  [ericjohnson](https://wordpress.org/support/users/ericjohnson/)
 * (@ericjohnson)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/querying-posts-based-on-category/)
 * Hi,
 * I need to query individual posts for different category pages.
 * I have the code:
 * `<?php if ( have_posts() && is_category('5') ) : query_posts( 'p=644' ); ?>`
 * But I need it to have nested if statements to check for each category. I have
   tried the if statement with the inline with the following code:
 *     ```
       <?php
       if ( have_posts() ) {
       query_posts( 'p=644' );
       }
       ?>
       ```
   
 * I am getting an error with this basic code, and I haven’t even begun to nest 
   if statements yet!
 * I wanted it to look something like:
 *     ```
       <?php
       if ( have_posts() ) {
       if ( is_category('5') ) {
       query_posts( 'p=644' );
       } elseif ( is_category('6') ) {
       query_posts('p=645' );
       }
       }
       ?>
       ```
   
 * Can someone please explain why the code above does not work, or suggest a better
   alternative to nesting it?

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

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/querying-posts-based-on-category/#post-3269474)
 * using the category ID, try
 * `is_category(5)`
 * or use the cat slug
 * `is_category('what-ever-cat')`
 *  [racer x](https://wordpress.org/support/users/racer-x-1/)
 * (@racer-x-1)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/querying-posts-based-on-category/#post-3269476)
 * Make sure you are following the order of using [query_posts](http://codex.wordpress.org/Function_Reference/query_posts).
   is_category() can be used before you run query_posts(). That way, you can alter
   the main query properly.
 * Here is a basic example:
 *     ```
       <?php
       // first detect the category
       if ( is_category(5) ) {
       $args = 'p=644';
       } elseif ( is_category(6) ) {
       $args = 'p=645';
       } else {
       // $args = something else or default action...
       }
       // next alter the main query
       query_posts( $args );
       // now start your loop
       if ( have_posts() )... etc..
       ```
   
 *  Thread Starter [ericjohnson](https://wordpress.org/support/users/ericjohnson/)
 * (@ericjohnson)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/querying-posts-based-on-category/#post-3269479)
 * Hello,
 * Thank you both for the reply!
 * I have tried racer x’s (btw, Paul Gilbert is a god) code condensed as:
 *     ```
       <?php
       // first detect the category
       	if ( is_category(5) ) {
       		$args = 'p=644';
       	}
       	// next alter the main query
       	query_posts( $args );
       ?>
       ```
   
 * And I am still getting a:
 * Parse error: syntax error, unexpected T_ELSE
 * `<?php if ( have_posts() && is_category('5') ) : query_posts( 'p=644' ); ?>`
 * Still works fine, so I am confused as hell!
 *  [racer x](https://wordpress.org/support/users/racer-x-1/)
 * (@racer-x-1)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/querying-posts-based-on-category/#post-3269480)
 * When I said “etc.” I should have clarified that you just use the regular start
   of a loop right after you use query_posts( $args ).
 * `if (have_posts()) : while (have_posts()) : the_post();`
 * When you are using query_posts you are altering the MAIN loop for the page. So
   there is no need for “have_posts() && is_category(‘5’) )”.
 * BTW – I actually never heard of Paul Gilbert, nor knew about a band called Racer
   X! Ha! Even though I have played guitar professionally at gigs for years. Racer
   X was from a childhood cartoon I watched called Speed Racer.
 *  Thread Starter [ericjohnson](https://wordpress.org/support/users/ericjohnson/)
 * (@ericjohnson)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/querying-posts-based-on-category/#post-3269481)
 * Ha I see! I suppose every time I head Racer X I just think of the band.
 * Regarding the code, I tried adding the if (have_Posts()) : to the front of my
   loop (before it was just while (have_posts()) : the_posts(); ), and I no longer
   get a parse error, but now it shows every single post whenever it does not enter
   a conditional!
 * Is there a way to null my $args to show no posts? I am thinking I just need to
   plug that into my else statement.
 * I actually tried changing $args to an arbitrary variable ($posts), but the query
   still uses all posts!
 * Thanks!
 *  [racer x](https://wordpress.org/support/users/racer-x-1/)
 * (@racer-x-1)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/querying-posts-based-on-category/#post-3269482)
 * When you are using both if(have_posts()) and while (have_posts()) then you need
   to make sure you are closing BOTH the “if” and “while” properly.
 *     ```
       if (have_posts()) : while (have_posts()) : the_post();
       // do stuff
       endwhile; endif;
       ```
   
 * If you are just using while (have_posts()) then you only need the closing “endwhile;”.
 * If you don’t want a loop to run as a default if the specified categories are 
   not found, then just bypass the whole thing like so:
 *     ```
       <?php
       if ( is_category(5) ) {
       $args = 'p=644';
       } elseif ( is_category(6) ) {
       $args = 'p=645';
       }
       if ( $args ) {
       query_posts( $args );
       if (have_posts()) : while (have_posts()) : the_post();
       // do your post loop
       endwhile;
       else :
       // sorry, no posts found
       endif;
       } // end if $args were found
       ```
   
 * Although, I am not fully understanding the logic here because this means you 
   have an entire category(s) that will only display one post if queried? That does
   not sound like a use for categories then which are for a “collection” of posts.
   I may be misunderstanding your situation though.
 * Perhaps using a [category template](http://codex.wordpress.org/Template_Hierarchy)
   would be better?
 * PS – I’ll check out the band anyway!
 *  Thread Starter [ericjohnson](https://wordpress.org/support/users/ericjohnson/)
 * (@ericjohnson)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/querying-posts-based-on-category/#post-3269483)
 * Hi Racer X,
 * Thanks for the reply! You are right; my decision to show one post per category
   was a poor decision on taxonomy (hard explain unless you saw the nature of the
   site). I have decided just to use a custom post template to achieve what I was
   wanted.
 * However, your explanations taught me about WordPress syntax so it was much appreciated.
 * Thanks for your time!
 * Btw, I hope you like 80’s speed metal!
 *  [racer x](https://wordpress.org/support/users/racer-x-1/)
 * (@racer-x-1)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/querying-posts-based-on-category/#post-3269484)
 * I figured it was one of those “situations” with taxonomies or whatever. I fall
   into that as well.
 * I love 80’s rock! That’s the era I grew up in. I was more standard metal than
   speed metal though (VH, Rush, etc.).
 * Best of luck!

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

The topic ‘Querying posts based on category’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 8 replies
 * 3 participants
 * Last reply from: [racer x](https://wordpress.org/support/users/racer-x-1/)
 * Last activity: [13 years, 6 months ago](https://wordpress.org/support/topic/querying-posts-based-on-category/#post-3269484)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
