0

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
) );
  • random anything is very bad for caching. if you have no preference about which terms to display, the latest N is as random to the user of the site as any other set of terms ;) – Mark Kaplun Jun 14 '17 at 14:50
5

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 );

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.