WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to get a random output in the taxonomy archive for my custom taxonomy "areas"

I have the taxonomy-areas.php in which I have a standard loop. This one displays the posts from 3 custom-post-types: Hotels Pubs Restaurants

As soon as I try to get them randomly with

<?php query_posts("orderby=rand"); ?>

it stops working, and starts to display standard posts as in index.php.

I've tryed to add parameters to query posts, but it degenerates the loop, that doesn't displays anymore the right archive for the taxonomy of course. How can I fix it? I would like to avoid to create a taxonomy-aree-$term.php for each and every single area I'm going to define in my custom-taxonomy...!

After some attempts I've tried with this one,

<?php query_posts("posts_per_page=24&orderby=rand&areas=north-east"); ?>

and tried to make it dynamic within the current taxonomy with this:

<?php query_posts("posts_per_page=24&orderby=rand&areas=$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); echo $term->slug;"); ?>

But with a blank output.

Thanks and best regards.

share|improve this question
up vote 1 down vote accepted

I've finally found the solution thanks to a friend of mine:



we can set the object name in the script as follows:


    'tax_query' => array(
            'taxonomy' => 'areas',
            'field' => 'slug',
            'terms' => $obj->name
        'orderby' => 'rand'


Hope this can help.

Enjoy! :)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.