Is it possible to get random terms? To get random posts you could use WP_Query
and set 'orderby' => 'rand'
.
But is there any way to do that with terms?
I’ve tried this:
$terms = get_terms( array(
'taxonomy' => 'webshops',
'hide_empty' => false,
'orderby' => 'rand',
'number' => 6
) );
1 Answer
Unlike a normal WP_Query()
, get_terms()
or WP_Term_Query()
does not have random ordering. You would either need to do it in SQL yourself or grab all the terms and shuffle them, pulling out 6 to make your random term array:
// Get all terms
$terms = get_terms( array(
'taxonomy' => 'webshops',
'hide_empty' => false,
) );
// Randomize Term Array
shuffle( $terms );
// Grab Indices 0 - 5, 6 in total
$random_terms = array_slice( $terms, 0, 6 );