user3250715
user3250715

Reputation: 51

Random order in WordPress Loop?

I'm using the basic loop code in a taxonomy archive (artists) and I was wondering how you can set the loop to show posts in random order ('orderby'=>'rand') it doesn't seem to work when I add the array? Any help would be great!

        <?php
                // Start the Loop.
                while ( have_posts() ) : the_post();

                    /*
                     * Include the post format-specific template for the content. If you want to
                     * use this in a child theme, then include a file called called content-___.php
                     * (where ___ is the post format) and that will be used instead.
                     */
                    array ( 'orderby' => 'RAND' );
                    get_template_part( 'content', get_post_format() );

                endwhile;
                // Previous/next page navigation.
                twentyfourteen_paging_nav();

            else :
                // If no content, include the "No posts found" template.
                get_template_part( 'content', 'none' );

            endif;
        ?>

Upvotes: 4

Views: 14671

Answers (5)

contempoinc
contempoinc

Reputation: 25

Try this:

<?php

$args = array(
    'orderby' => 'rand'
);
$query = new WP_Query($args);

if (have_posts()) {     

    while ( $query->have_posts() ) : $query->the_post();

        get_template_part( 'content', get_post_format() );

    endwhile;
    // Previous/next page navigation.
    twentyfourteen_paging_nav();

else :
    // If no content, include the "No posts found" template.
    get_template_part( 'content', 'none' );

endif;

?>

Upvotes: -1

Paresh Shingala
Paresh Shingala

Reputation: 146

query_posts(array( 'showposts' => 6, 'orderby' => 'rand', 'category_name' => 'News' //You can insert any category name ));

Upvotes: 1

Ravi Patel
Ravi Patel

Reputation: 5211

 <?php  

$query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '-1' ) );

        if( $query->have_posts() ):
                // Start the Loop.
                while ( $query->have_posts() ) : $query->the_post();

                    /*
                     * Include the post format-specific template for the content. If you want to
                     * use this in a child theme, then include a file called called content-___.php
                     * (where ___ is the post format) and that will be used instead.
                     */

                    get_template_part( 'content', get_post_format() );

                endwhile;
                // Previous/next page navigation.
                twentyfourteen_paging_nav();

            else :
                // If no content, include the "No posts found" template.
                get_template_part( 'content', 'none' );

            endif;
        ?>

more info for query

Upvotes: 10

RRikesh
RRikesh

Reputation: 14381

You have two ways of doing it. The first way is not the best way, but it may be simpler for you to understand:

Using WP_Query

<?php

$args = array(
    'orderby' => 'random'
    );

$query = new WP_Query( $args );

if( $query->have_posts() ):
    // Start the Loop.
    while ( $query->have_posts() ) : $query->the_post();

        get_template_part( 'content', get_post_format() );

    endwhile;
    // Previous/next page navigation.
    twentyfourteen_paging_nav();

else :
    // If no content, include the "No posts found" template.
    get_template_part( 'content', 'none' );

endif;

Here, we'll be using a custom WP_Query object and orderby to get random posts.


Using pre_get_posts

The best way to do it is by using the pre_get_post action to modify the page output automatically. You might need some more coding though.

Upvotes: -2

user3085713
user3085713

Reputation: 11

Nice Question first !

You can do that with simple using function of PHP. http://www.php.net/manual/en/function.shuffle.php

Follow below step:

  1. First thing is get all post with query
  2. You know that wordpress will provide the result in array format. So, do not try more coding its too complecated.
  3. Now you have array of result so just use PHP function shuffle. http://www.php.net/manual/en/function.shuffle.php

Please ask me after implementation if any query.

Thanks !

Upvotes: -1

Related Questions