Title: Random Post Order
Last modified: August 19, 2016

---

# Random Post Order

 *  [sootylad](https://wordpress.org/support/users/sootylad/)
 * (@sootylad)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/random-post-order-1/)
 * hi,
 * Before I start I haven’t used this forum before and when i asked this question
   someone closed the thread? i’m assuming it was accidental so i’ve posted it again…
   If I am indeed doing something wrong could you let me know as it is very important
   that I can use this resource as there are deadlines involved etc…
 * I’m trying to get posts to list randomly on only one top level category page…
 * I have a category called portfolio, and this has plenty of sub categories which
   all use the same category-4.php template file (by using the Elevate Parent Category
   plugin ([http://pluginscatalog.com/plugins/elevate-parent-category-template.html](http://pluginscatalog.com/plugins/elevate-parent-category-template.html))
 * now, when viewing the main portfolio page (NOT burrowing down any deeper in the
   hierarchy) i want the posts displayed in a random order…
 * i can’t use code such as:
 * <?php $news = new WP_Query(“cat=4&orderby=rand”); ?>
 * as this will just display a random load of portfolio posts from all sub sections
   even when i am IN a sub section (it does however give me the correct effect when
   i’m on the top level portfolio page)
 * so basically i need code that will: (in english)
 * if the category is 4, display the posts from all portfolio subsections in a random
   order, if not (i.e. you’re in a subsection) display as normal using a standard
   loop.
 * I hope this makes sense? help would be much appreciated…

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

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/random-post-order-1/#post-972919)
 * Instead of creating a new query, why not just do this before your [loop](http://codex.wordpress.org/The_Loop)
   in your category archive:
 * `query_posts($query_string . '&orderby=rand');`
 * Here’s the category.php I’m using with the WordPress Default Theme:
    [http://wordpress.pastebin.ca/1321959](http://wordpress.pastebin.ca/1321959)
 * Note: I do no see a CLOSED topic in your profile:
    [http://wordpress.org/support/profile/1223254](http://wordpress.org/support/profile/1223254)
 *  Thread Starter [sootylad](https://wordpress.org/support/users/sootylad/)
 * (@sootylad)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/random-post-order-1/#post-973002)
 * oh… when i tried to reply to the last one is said sorry this thread is closed?
   is was an error message on a dark page with “bb” at the top…
 * i’ll have a go at what you just recommended… – many thanks 🙂
 *  Thread Starter [sootylad](https://wordpress.org/support/users/sootylad/)
 * (@sootylad)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/random-post-order-1/#post-973015)
 * that is great thanks 🙂 didn’t know you could do that! (well… obviously thats
   why i asked :))
 * is there some code i could surround the query_posts bit with to make it check
   if its only on the top level (i.e. category 4) and not one of the sub-categories
   of category 4?
 * the results of the sub sections i don’t want randomised you see 🙂
 *  Thread Starter [sootylad](https://wordpress.org/support/users/sootylad/)
 * (@sootylad)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/random-post-order-1/#post-973062)
 * right, MichaelH helped me in another thread about the category identification
   🙂 – but i’m running into problems i think because of the Elevate Parent Category…
 * what i am using is this:
 *     ```
       if (is_category()) {
       $cat = get_query_var('cat');
       if ($cat == 4) {
       echo $cat;
       query_posts($query_string . '&orderby=rand');
       }
       }
       ```
   
 * it it does indeed randomise the posts, but it is still also randomising the posts
   in the sub sections (non category 4) – i added in echo $cat to confirm what i
   thought and even when im in a subsection, and the posts displayed are indeed 
   the correct posts (albeit in a random order) the cat is still showing as “4”
 * as far as i know the plugin i’m using is simply alowing sub categories of 4 to
   use the same skin (category-4.php) so it shouldn’t be affecting which category
   it thinks it is… should it?
 * (could I just say MichaelH, you are being very helpful thanks very much :))

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

The topic ‘Random Post Order’ is closed to new replies.

 * 4 replies
 * 2 participants
 * Last reply from: [sootylad](https://wordpress.org/support/users/sootylad/)
 * Last activity: [17 years, 4 months ago](https://wordpress.org/support/topic/random-post-order-1/#post-973062)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
