wordpress developers - advice on how to random posts from all categories

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mineistaken
    See signature :)
    • Apr 2007
    • 29656

    #1

    wordpress developers - advice on how to random posts from all categories

    How to random display posts from all categories, most importantly when index.php is not a home page, since home page is static page.
    Any solution?
  • mineistaken
    See signature :)
    • Apr 2007
    • 29656

    #2
    any WP guru's ?

    Comment

    • alcstrategy
      Confirmed User
      • May 2012
      • 124

      #3
      as with anything there's many ways to skin a cat, but there's an example in wordpress docs here: http://codex.wordpress.org/Template_Tags/get_posts

      numberposts is the number of posts you want
      orderby is rand which is equiv to mysql rand() which is random num

      the docs show other parameters you can use also

      i guess you could put this in page.php, although I guess it depends where you want it, etc, but i just posted general example


      <ul>
      <?php
      $args = array( 'numberposts' => 5, 'orderby' => 'rand' );
      $rand_posts = get_posts( $args );
      foreach( $rand_posts as $post ) : ?>
      <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
      <?php endforeach; ?>
      </ul>
      Last edited by alcstrategy; 06-04-2013, 11:40 AM.

      Comment

      • mineistaken
        See signature :)
        • Apr 2007
        • 29656

        #4
        Thanks. Would this ^ display random posts from all categories combined?

        Comment

        • alcstrategy
          Confirmed User
          • May 2012
          • 124

          #5
          i believe it will query posts from any category if that's what you mean
          you can set categories with the "category" param

          Comment

          • mineistaken
            See signature :)
            • Apr 2007
            • 29656

            #6
            Originally posted by alcstrategy
            i believe it will query posts from any category if that's what you mean
            you can set categories with the "category" param
            To be clear, I mean from "all" categories.
            Lets say there are 3 categories - blondes, brunettes and redheads.
            I want to have page (not home page) where all posts from all 3 categories would be displayed at random order.

            Comment

            • Dankasaur
              So Fucking Fossilized
              • Sep 2011
              • 1432

              #7
              Originally posted by mineistaken
              To be clear, I mean from "all" categories.
              Lets say there are 3 categories - blondes, brunettes and redheads.
              I want to have page (not home page) where all posts from all 3 categories would be displayed at random order.
              You'd have to loop through all your categories and pull them, and then pull 3 random posts from each category.

              Comment

              • EddyTheDog
                Just Doing My Own Thing
                • Jan 2011
                • 25433

                #8
                Originally posted by mineistaken
                To be clear, I mean from "all" categories.
                Lets say there are 3 categories - blondes, brunettes and redheads.
                I want to have page (not home page) where all posts from all 3 categories would be displayed at random order.

                I am not saying that code would work - I haven't tried it - If it does then it would show from all cats...

                Comment

                • EddyTheDog
                  Just Doing My Own Thing
                  • Jan 2011
                  • 25433

                  #9
                  Originally posted by Dankasaur
                  You'd have to loop through all your categories and pull them, and then pull 3 random posts from each category.
                  Wouldn't it use all posts and then randomize them?.....

                  Comment

                  • mineistaken
                    See signature :)
                    • Apr 2007
                    • 29656

                    #10
                    Originally posted by EddyTheDog
                    Wouldn't it use all posts and then randomize them?.....
                    Thats what I need. Pull all posts from all categories and display them all at random order

                    Comment

                    • alcstrategy
                      Confirmed User
                      • May 2012
                      • 124

                      #11
                      it queries all posts and returns random order but will limit to 5 because of the numberposts paramater
                      Last edited by alcstrategy; 06-04-2013, 12:15 PM. Reason: was new reply on refresh

                      Comment

                      • Dankasaur
                        So Fucking Fossilized
                        • Sep 2011
                        • 1432

                        #12
                        Originally posted by EddyTheDog
                        Wouldn't it use all posts and then randomize them?.....
                        No because you'd pass the category off to the query for posts and it'd only pull random ones in that category.

                        Comment

                        • BSleazy
                          Confirmed User
                          • Aug 2002
                          • 6721

                          #13
                          You should be able to do something like this

                          Code:
                          <?php $posts = get_posts('orderby=rand&numberposts=10'); foreach($posts as $post) { ?>
                          icq 156131086

                          Comment

                          Working...