• Hi

    Im trying to get list a custom taxonomy category list with a random order. So that every time you refresh the page, a new order appears.

    So far i can list the custom taxonomy but I can’t get the random orderly to work.

    Any help would be appreciated.


    <?php

    foreach (get_terms('your_taxonomy' ,
    array(
    "orderby" => "rand",
    "number" => 5,
    'hide_empty' => 1,
    'exclude' => array($cat->term_id),

    )) as $cat) :

    ?>

    <li class='post'>
    slug, 'your_taxonomy'); ?>"><div class='thumb-post'>

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

    </div>
    <h2> <?php echo $cat->name; ?></h2>
    <p class='meta'>
    Directed By <?php echo single_cat_title('' , false); ?>
    </p>

    <?php endforeach; ?>

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Unfortunately, get_terms() does not recognize the orderby “rand” directive in the same way that other WP functions like get_posts() do. You can feed a SQL ORDER BY clause directly into the query with the ‘get_terms_orderby’ filter. The value returned must be valid mySQL syntax, so from your filter callback, return the SQL function RAND(), not simply the string “rand”.

    Remove your filter callback when you are done with it so that using get_terms() elsewhere in WP doesn’t result in random terms.

Viewing 1 replies (of 1 total)
  • The topic ‘get_terms orderby random not working’ is closed to new replies.