Support » Fixing WordPress » wp_query orderby random not working

  • Can’t for the life of me get the orderby to be random in this wp_query. ‘rand’ not working at all.

    Can anyone see why this code is still producing a list of posts in standard reverse chronological order instead of randomized mix?

    function homex_top10_loop() {
    	$args = array(
    		'posts_per_page' => 10,
    		'post_type' => 'post',
    		'cat' => 4,
    		'orderby' => 'rand'
    	);
    	//print_r($args);
    	global $wp_query;
    	$wp_query = new WP_Query( $args );
Viewing 15 replies - 1 through 15 (of 16 total)
  • Moderator t-p

    (@t-p)

    see if info in this codex helps: http://codex.wordpress.org/Class_Reference/WP_Query

    thanks t-p… that’s where i found the sample code this is based on.

    for the life of me, i could swear this snippet is following the EXACT protocols on that codex page, unless my eyes have gone numb to something wrong here. ???

    <sigh>

    For what it’s worth, I am running into the same exact problem.

    That is strange because the core query code is using:

    $orderby = 'RAND()';

    I use this in a template page of posts to reverse the posts in a list:

    <?php query_posts($query_string . "&order=ASC"); ?>

    So it should reorder them as well.

    Just a suggestion remove it from the arguments and try this just before the have_posts(), it should apply a filter and return the lines and query the returned recordset.

    <?php query_posts($query_string . "&orderby=rand"); ?>
    
    <?php if ( have_posts() ) : ?>

    EDIT:
    Tested the above in a twenty eleven child theme’s archive.php and when I viewed the monthly archive the posts were random!

    HTH

    David

    Thanks. This isn’t working for me either. Maybe part of the problem is that I am actually doing this outside of WordPress. My PHP script has this to include WordPress:

    require('/home/dir/public_html/wp-load.php');

    Everything else works fine, except for the sorting. Weird.

    Jason Paul

    (@jasontrasaterracom)

    I’m having quite a time with this too. Nothing I try with WP_Query will output a list of random posts. Any additional advice? Here’s What my code looks like:

    <?php
    global $wp_query;
    query_posts(
    	array_merge(
    		$wp_query->query,
    		array(
    		//'order' => 'ASC',
    		//'orderby'=>'rand', 'none',
    		'posts_per_page' => '3',
    		'tax_query'	=> array(
            array(
                'taxonomy'  => 'category',
                'field'     => 'slug',
                'terms'     => array( 'featured'),// exclude media posts in the news-cat custom taxonomy
                'operator'  => 'IN')
                ),
    
    		)
    	)
    ); ?>
    
    <?php if (have_posts ( $query_string . "&orderby=rand" )) : while (have_posts()) : the_post(); ?>
    Jason Paul

    (@jasontrasaterracom)

    Forgot to add notify on this.

    running into the same issue here. orderby=rand is simply not working.

    Keeping an eye out here for a possible solution.

    Jason Paul

    (@jasontrasaterracom)

    I was developing a site on WP Engine. They actually disable the Orderby Rand Function. If you happen to be using WP Engine, in the dashboard area they put in the WP Admin go there scroll down and make sure the Orderby Rand function is not disabled. You would not believe what I went through before I deduced that it was the host causing the problem.

    Jason, you are a gem! This was a WP Engine site and I was just about to loose my mind trying to figure out what was causing it.

    THANK YOU!

    t-p you help me a lot! thanks

    here is the code that i used..

    just want to share..

    <?php $loop = new WP_Query( array( orderby => 'rand', 'post_type' => 'testimonials', 'posts_per_page' => 1 ) ); ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <?php the_permalink();?>

    Thanks! It is indeed a server-side setting.

    I’m using the same code as Melster and not seeing a random post. I’ll keep poking around and see what I can find. For now, here’s my code:

    <?php $args = array(
        'orderby' => 'rand',
        'post_type' => 'testimonial',
    	'posts_per_page' => 1
    );
    // The Query
    $the_query = new WP_Query( $args );
    
    // The Loop
    if($the_query->have_posts()) : while($the_query->have_posts()) : $the_query->the_post(); ?>
    
    <div class="testimonial-slide">
      <blockquote>
        <p class="testimonial-title"><?php echo get_post_meta($post->ID, 'testimonial_heading', true); ?></p>
        <?php the_content(); ?>
        <p class="attribution"><?php echo get_the_title(); ?></p>
      </blockquote>
    </div>
    <?php endwhile; endif; ?>

    OK – so it turns out I was using the Post Types Order plugin which removes the ability for random ordering unless you place:

    remove_all_filters('posts_orderby');

    before your query… like so:

    remove_all_filters('posts_orderby');
     $args = array(
        'orderby' => 'rand',
        'post_type' => 'testimonial',
    	'posts_per_page' => 1
    );
    // The Query
    $the_query = new WP_Query( $args );

    I got this solution from: How To Get Random Posts From WP Query. I hope this helps someone!

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘wp_query orderby random not working’ is closed to new replies.