Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

1 Answer 1

up vote 0 down vote accepted

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

exploiting

get_queried_object()

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

<?php
    $obj=$wp_query->get_queried_object();

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

    query_posts($args);
?>

Hope this can help.

Enjoy! :)

share|improve this answer
add comment

Your Answer

 
discard

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.