I'm trying to display one of my custom taxonomy lists on a page.

I want to show five but have them in different random orders when the page is refreshed.

I've tried using shuffle, but it just shuffles the first 5 and you dont see any of the the others.

I tried orderby RAND but it doesnt work. This is my code so far.

$all_artists = get_terms( 'artists', array(
"number" => 5,                           
'hide_empty' => 1,
'exclude' => array($cat->term_id),                      
) );          

shuffle( $all_artists );
foreach ($all_artists as $cat) :?>

<li class='post'>
<a href="<?php echo get_term_link($cat->slug, 'directors'); ?>">
<div class='thumb-post'>

<img src="<?php echo z_taxonomy_image_url($cat->term_id, 'poster'); ?>" />

<h2> <?php echo $cat->name; ?></h2>
<p class='meta'>

 <?php endforeach; ?>
  • Try removing number parameter in the argument. In default all terms are returned. About ordering, there is no option to Random order. See official documentation – Nilambar Sharma Sep 4 '14 at 4:43
  • Welcome to WPSE and the stackexchange network. If you haven't done so, please read our tour page to get an idea how our and other communities operate. Looking forward to your positive inputs. :-) – Pieter Goosen Sep 4 '14 at 6:13

There is no default way to sort terms randomly. There are ways to do this using php.

First, you'll need to remove the number argument from get_terms. As your code currently stands, you are getting 5 terms and shuffling them around.

For this to work, you'll need to retrieve all the terms from your taxonomy, shuffle that returned array randomly with the php function shuffle(), pass that through a foreach loop, create a new array and then use array_slice to to get the first 5 entries

Here is just a rough idea

$all_artists = get_terms( 'category' );          
shuffle( $all_artists );

$term_names = [];
foreach ( $all_artists as $cat )
    $term_names[] = $cat->name;

$output = array_slice( $term_names, 0, 5 );

foreach ( $output as $key=>$value )
     echo '<p>' . $value . '</p>';


Here is another way to achieve the same. This involves creating a custom function that will sort the array randomly, and keeping the key/value pairs in place. This function comes from this answer on SO by @karim79

You can then use array_slice() to get the first % key/value pairs

function shuffle_assoc( $list ) 
    if ( !is_array( $list ) ) 
        return $list; 

    $keys = array_keys( $list ); 
    shuffle( $keys ); 

    $random = []; 
    foreach ( $keys as $key ) 
        $random[$key] = $list[$key]; 
    return $random; 

$terms  = get_terms( 'category' );  
$output = array_slice( shuffle_assoc( $q ), 0, 5 );
?><pre><?php var_dump($output)); ?></pre><?php
  • Just checking in to see what is the status of your problem, is it resolved yet. If my answer solved your issue, please consider accepting my answer. If mine did not meet your standards and you manage to resolve this issue yourself, please consider posting and accepting your own answer. Would be nice to know how you've resolved this :-). If you are still having this problem, feel free to post a comment to my answer explaining where my answer are lacking. Many thanks – Pieter Goosen Sep 7 '14 at 12:15
  • Thank you for the working code only i have one question. It rendered only the first 4 categories and started displaying same 4 categories in random order. ANy ideas @Pieter Goosen ? – Anahit DEV Apr 4 '16 at 14:17
  • @AnahitDEV It works perfectly fine on my side. You might have some caching going on somewhere. You also need to remember, you will have some terms repeated between page loads, unfortunately this is the nature of random ordering. Each request is a brand new one – Pieter Goosen Apr 4 '16 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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