WordPress.org

Ready to get started?Download WordPress

Forums

wp_query orderby random not working (17 posts)

  1. hyperhead
    Member
    Posted 2 years ago #

    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 );
  2. Tara
    Member
    Posted 2 years ago #

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

  3. hyperhead
    Member
    Posted 2 years ago #

    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>

  4. Bill Dennen
    Member
    Posted 1 year ago #

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

  5. Digital Raindrops
    Member
    Posted 1 year ago #

    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

  6. Bill Dennen
    Member
    Posted 1 year ago #

    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.

  7. Jason Paul
    Member
    Posted 1 year ago #

    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(); ?>
  8. Jason Paul
    Member
    Posted 1 year ago #

    Forgot to add notify on this.

  9. phases
    Member
    Posted 1 year ago #

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

    Keeping an eye out here for a possible solution.

  10. Jason Paul
    Member
    Posted 1 year ago #

    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.

  11. phases
    Member
    Posted 1 year ago #

    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!

  12. melster
    Member
    Posted 1 year ago #

    t-p you help me a lot! thanks

  13. melster
    Member
    Posted 1 year ago #

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

  14. Josh Fialkoff
    Member
    Posted 1 year ago #

    Thanks! It is indeed a server-side setting.

  15. Devin Walker
    Member
    Posted 1 year ago #

    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; ?>
  16. Devin Walker
    Member
    Posted 1 year ago #

    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!

  17. lkraav
    Member
    Posted 1 year ago #

    Wow thanks for the tip Devin. That was exactly the culprit here.

Topic Closed

This topic has been closed to new replies.

About this Topic