logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$8
Randomize Posts

I'm trying to randomize the posts shown on my homepage, which are being pulled from specific categories. Take a look, and let me know if you need more info. https://laxaxis.com

This question has been answered.

Patrick MacAdams | 06/16/11 at 10:31am Edit


(5) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    06/16/11
    10:40am
    Julio Potier says:

    Hello

    You can make a query_post with shiffle like this :

    <?php shuffle(query_posts('author=1')); ?>

    or random
    <?php query_posts('author=1&orderby=rand'); ?>

    or other random
    <?php query_posts(array('author' => '1', 'orderby' => 'rand')); ?>

    Done !

    Previous versions of this answer: 06/16/11 at 10:37am | 06/16/11 at 10:39am | 06/16/11 at 10:40am

  • avatar
    Last edited:
    06/16/11
    10:36am
    Erez S says:

    orderby=rand
    in your query

  • avatar
    Last edited:
    06/16/11
    10:39am
    Fahd Murtaza says:

    Place


    query_posts(array('orderby' => 'rand', 'showposts' => 1));


    before your


    if (have_posts()) :



    and replace the 1 is showposts by 1.

    Previous versions of this answer: 06/16/11 at 10:39am | 06/16/11 at 10:39am

    • 06/16/11 10:47am

      Fahd Murtaza says:

      Also post your code for us to look at.

    • 06/16/11 10:48am

      Patrick MacAdams says:

      <?php 

      echo "<div class='wrapper' style='height:780px;'><br /><h1>TOURNAMENTS</h1><br />";

      $count1 = 0;

      // The Query for tournament cat id 3
      $the_query = new WP_Query("cat=3&showposts=11");

      // The Loop
      query_posts(array('orderby' => 'rand', 'showposts' => 1));
      while ( $the_query->have_posts() ) : $the_query->the_post();

      if($count1==0){
      echo cooolzine_index_toparticle($post);
      }else{
      echo cooolzine_index_boxarticle($post);
      }

      $count1++;

      endwhile;

      // Reset Post Data
      wp_reset_postdata();

      if (get_option(get_current_theme().'_adside') == 'true') {
      if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Homepage1')) {}
      } // Sidebar Homepage 1

      $cat_link1 = get_category_link(3);

      echo "</div><h2><a href='$cat_link1' style='float:right;'>See More Tournaments...</a></h2><br/>";



      echo "<div class='wrapper' style='height:420px;'><br /><h1>CAMPS</h1><br />";

      $count2 = 0;

      // The Query for Camps cat id 4
      $the_query = new WP_Query("cat=4&showposts=5");

      // The Loop
      while ( $the_query->have_posts() ) : $the_query->the_post();

      if($count2==0){
      echo cooolzine_index_toparticle($post);
      }else{
      echo cooolzine_index_boxarticle($post);
      }

      $count2++;

      endwhile;

      // Reset Post Data
      wp_reset_postdata();

      if (get_option(get_current_theme().'_adside') == 'true') {
      if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Homepage2')) {}
      } // Sidebar Homepage 2

      $cat_link2 = get_category_link(4);

      echo "</div><h2><a href='$cat_link2' style='float:right;'>See More Camps...</a></h2>";




      echo "<div class='wrapper' style='height:420px;'><br /><h1>CLUBS</h1><br />";

      $count3 = 0;

      // The Query for clubs cat id 6
      $the_query = new WP_Query("cat=6&showposts=5");

      // The Loop
      while ( $the_query->have_posts() ) : $the_query->the_post();

      if($count3==0){
      echo cooolzine_index_toparticle($post);
      }else{
      echo cooolzine_index_boxarticle($post);
      }

      $count3++;

      endwhile;

      // Reset Post Data
      wp_reset_postdata();

      if (get_option(get_current_theme().'_adside') == 'true') {
      if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Homepage3')) {}
      } // Sidebar Homepage 3

      $cat_link3 = get_category_link(6);

      echo "</div><h2><a href='$cat_link3' style='float:right;'>See More Clubs...</a></h2>";




      echo "<div class='wrapper' style='height:420px;'><br /><h1>CLINICS</h1><br />";

      $count4 = 0;

      // The Query for Clinics cat id 5
      $the_query = new WP_Query("cat=5&showposts=5");

      // The Loop
      while ( $the_query->have_posts() ) : $the_query->the_post();

      if($count4==0){
      echo cooolzine_index_toparticle($post);
      }else{
      echo cooolzine_index_boxarticle($post);
      }

      $count4++;

      endwhile;

      // Reset Post Data
      wp_reset_postdata();

      if (get_option(get_current_theme().'_adside') == 'true') {
      if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Homepage4')) {}
      } // Sidebar Homepage 4

      $cat_link4 = get_category_link(5);

      echo "</div><h2><a href='$cat_link4' style='float:right;'>See More Clinics...</a></h2>";




      echo "<div class='wrapper' style='height:420px;'><br /><h1>LEAGUES</h1><br />";

      $count5 = 0;

      // The Query for Leagues cat id 6
      $the_query = new WP_Query("cat=44&showposts=1");

      // The Loop
      while ( $the_query->have_posts() ) : $the_query->the_post();

      if($count5==0){
      echo cooolzine_index_toparticle($post);
      }else{
      echo cooolzine_index_boxarticle($post);
      }

      $count5++;

      endwhile;

      // Reset Post Data
      wp_reset_postdata();

      if (get_option(get_current_theme().'_adside') == 'true') {
      if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Homepage5')) {}
      } // Sidebar Homepage 5

      $cat_link5 = get_category_link(44);

      echo "</div><h2><a href='$cat_link5' style='float:right;'>See More Leagues...</a></h2>";




      echo "<div class='wrapper' style='height:420px;'><br /><h1>SCHOOLS</h1><br />";

      $count6 = 0;

      // The Query for schools cat id
      $the_query = new WP_Query("cat=7&showposts=1");

      // The Loop
      while ( $the_query->have_posts() ) : $the_query->the_post();

      if($count6==0){
      echo cooolzine_index_toparticle($post);
      }else{
      echo cooolzine_index_boxarticle($post);
      }

      $count6++;

      endwhile;

      // Reset Post Data
      wp_reset_postdata();

      if (get_option(get_current_theme().'_adside') == 'true') {
      if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Homepage6')) {}
      } // Sidebar Homepage 6

      $cat_link6 = get_category_link(7);

      echo "</div><h2><a href='$cat_link6' style='float:right;'>See More Schools...</a></h2>";

      ?>

    • 06/16/11 11:52am

      Fahd Murtaza says:

      Your new code, please try it

      <?php 
      echo "<div class='wrapper' style='height:780px;'><br /><h1>TOURNAMENTS</h1><br />";

      $count1 = 0;

      // The Query for tournament cat id 3

      $args = array(
      'cat' => 3,
      'ignore_sticky_posts' => 1,
      'post__not_in' => $sticky,
      'paged' => $paged,
      'orderby' => 'rand',
      'order' => 'DESC'
      'showposts'=>'1'
      );

      $the_query = new WP_Query($args);

      // The Loop

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

      if($count1==0){

      echo cooolzine_index_toparticle($post);

      }else{

      echo cooolzine_index_boxarticle($post);

      }

      $count1++;

      endwhile;

      // Reset Post Data

      wp_reset_postdata();

      if (get_option(get_current_theme().'_adside') == 'true') {

      if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Homepage1')) {}

      } // Sidebar Homepage 1



      $cat_link1 = get_category_link(3);

      echo "</div><h2><a href='$cat_link1' style='float:right;'>See More Tournaments...</a></h2><br/>";

      echo "<div class='wrapper' style='height:420px;'><br /><h1>CAMPS</h1><br />";

      $count2 = 0;

      // The Query for Camps cat id 4

      $args2 = array(
      'cat' => 4,
      'ignore_sticky_posts' => 1,
      'post__not_in' => $sticky,
      'paged' => $paged,
      'orderby' => 'rand',
      'order' => 'DESC',

      );


      $the_query = new WP_Query($args);

      // The Loop

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

      if($count2==0){

      echo cooolzine_index_toparticle($post);

      }else{

      echo cooolzine_index_boxarticle($post);

      }

      $count2++;

      endwhile;

      // Reset Post Data

      wp_reset_postdata();

      if (get_option(get_current_theme().'_adside') == 'true') {

      if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Homepage2')) {}

      } // Sidebar Homepage 2

      $cat_link2 = get_category_link(4);

      echo "</div><h2><a href='$cat_link2' style='float:right;'>See More Camps...</a></h2>";


      echo "<div class='wrapper' style='height:420px;'><br /><h1>CLUBS</h1><br />";



      $count3 = 0;


      // The Query for clubs cat id 6

      $args3 = array(
      'cat' => 6,
      'ignore_sticky_posts' => 1,
      'post__not_in' => $sticky,
      'paged' => $paged,
      'orderby' => 'rand',
      'order' => 'DESC',
      'showposts' => 5

      );


      $the_query = new WP_Query($args3);



      // The Loop

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



      if($count3==0){

      echo cooolzine_index_toparticle($post);

      }else{

      echo cooolzine_index_boxarticle($post);

      }



      $count3++;



      endwhile;



      // Reset Post Data

      wp_reset_postdata();



      if (get_option(get_current_theme().'_adside') == 'true') {

      if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Homepage3')) {}

      } // Sidebar Homepage 3



      $cat_link3 = get_category_link(6);



      echo "</div><h2><a href='$cat_link3' style='float:right;'>See More Clubs...</a></h2>";









      echo "<div class='wrapper' style='height:420px;'><br /><h1>CLINICS</h1><br />";



      $count4 = 0;



      // The Query for Clinics cat id 5

      $args4 = array(
      'cat' => 5,
      'ignore_sticky_posts' => 1,
      'post__not_in' => $sticky,
      'paged' => $paged,
      'orderby' => 'rand',
      'order' => 'DESC',
      'showposts' => 5

      );


      $the_query = new WP_Query($args4);



      // The Loop

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



      if($count4==0){

      echo cooolzine_index_toparticle($post);

      }else{

      echo cooolzine_index_boxarticle($post);

      }



      $count4++;



      endwhile;



      // Reset Post Data

      wp_reset_postdata();



      if (get_option(get_current_theme().'_adside') == 'true') {

      if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Homepage4')) {}

      } // Sidebar Homepage 4



      $cat_link4 = get_category_link(5);



      echo "</div><h2><a href='$cat_link4' style='float:right;'>See More Clinics...</a></h2>";









      echo "<div class='wrapper' style='height:420px;'><br /><h1>LEAGUES</h1><br />";



      $count5 = 0;



      // The Query for Leagues cat id 6

      $args5 = array(
      'cat' => 44,
      'ignore_sticky_posts' => 1,
      'post__not_in' => $sticky,
      'paged' => $paged,
      'orderby' => 'rand',
      'order' => 'DESC',
      'showposts' => 1

      );

      //$the_query = new WP_Query("cat=44&showposts=1");

      $the_query = new WP_Query($args5);



      // The Loop

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



      if($count5==0){

      echo cooolzine_index_toparticle($post);

      }else{

      echo cooolzine_index_boxarticle($post);

      }



      $count5++;



      endwhile;



      // Reset Post Data

      wp_reset_postdata();



      if (get_option(get_current_theme().'_adside') == 'true') {

      if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Homepage5')) {}

      } // Sidebar Homepage 5



      $cat_link5 = get_category_link(44);



      echo "</div><h2><a href='$cat_link5' style='float:right;'>See More Leagues...</a></h2>";









      echo "<div class='wrapper' style='height:420px;'><br /><h1>SCHOOLS</h1><br />";



      $count6 = 0;



      // The Query for schools cat id

      $args5 = array(
      'cat' => 7,
      'ignore_sticky_posts' => 1,
      'post__not_in' => $sticky,
      'paged' => $paged,
      'orderby' => 'rand',
      'order' => 'DESC',
      'showposts' => 1

      );

      //$the_query = new WP_Query("cat=7&showposts=1");

      $the_query = new WP_Query($args5);





      // The Loop

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



      if($count6==0){

      echo cooolzine_index_toparticle($post);

      }else{

      echo cooolzine_index_boxarticle($post);

      }



      $count6++;



      endwhile;



      // Reset Post Data

      wp_reset_postdata();



      if (get_option(get_current_theme().'_adside') == 'true') {

      if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Homepage6')) {}

      } // Sidebar Homepage 6



      $cat_link6 = get_category_link(7);



      echo "</div><h2><a href='$cat_link6' style='float:right;'>See More Schools...</a></h2>";



      ?>

  • avatar
    Last edited:
    06/16/11
    10:38am
    Monster Coder says:

    check this api doc:
    http://codex.wordpress.org/Template_Tags/get_posts

    if you write

    'orderby' => 'rand',

    it should random posts

  • avatar
    Last edited:
    06/16/11
    11:59am
    Marko Nikolic says:

    You don't need to randomize it by order, but by category, right?

    Remove cat=xx& from code, from place you want to randomize it.


    xx = number of category.

This question has expired.



Patrick MacAdams voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.