get_post random and order by not working

Yes, this is the correct syntax:

$args = array(
    'orderby' => 'rand',
    'order'    => 'ASC'
);
query_posts( $args );

However plugins can keep this from working properly. Try disabling ALL plugins and see if that helps. Two known plugins which keep orderby=rand from working are Post Type Order and WP_Sticky

Also, if you have Post Types order installed make sure you visit the Admin page and check the settings. You can use this plugin and keep it from automatically re-ordering posts:

http://img829.imageshack.us/img829/2616/pictureot.png

And then you can use the code for Post Types Order to specifically order those posts in places where you need them to be ordered via the custom/menu-order. Here is the example code for that plugin:

The following PHP code will still return the post in the set-up Order:

$args = array(
'post_type' => 'feature'
);

$my_query = new WP_Query($args);
while ($my_query->have_posts())
{
$my_query->the_post();
(..your code..)          
}

Or:

$posts = get_posts($args);
foreach ($posts as $post)
{
(..your code..)     
}

If the Auto Sort is uncheck you will need to use the “orderby” and “order” parameters:

$args = array(
'post_type' => 'feature',
'orderby'   => 'menu_order',
'order'     => 'ASC'
);

Leave a Comment