GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   wordpress developers - advice on how to random posts from all categories (https://gfy.com/showthread.php?t=1111435)

mineistaken 06-04-2013 04:45 AM

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 06-04-2013 12:22 PM

any WP guru's ? :winkwink:

alcstrategy 06-04-2013 12:38 PM

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>

mineistaken 06-04-2013 12:48 PM

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

alcstrategy 06-04-2013 12:53 PM

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

mineistaken 06-04-2013 01:02 PM

Quote:

Originally Posted by alcstrategy (Post 19654696)
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.

Dankasaur 06-04-2013 01:04 PM

Quote:

Originally Posted by mineistaken (Post 19654713)
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.

EddyTheDog 06-04-2013 01:05 PM

Quote:

Originally Posted by mineistaken (Post 19654713)
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...

EddyTheDog 06-04-2013 01:07 PM

Quote:

Originally Posted by Dankasaur (Post 19654719)
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?.....

mineistaken 06-04-2013 01:10 PM

Quote:

Originally Posted by EddyTheDog (Post 19654725)
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 :)

alcstrategy 06-04-2013 01:13 PM

it queries all posts and returns random order but will limit to 5 because of the numberposts paramater

Dankasaur 06-04-2013 01:29 PM

Quote:

Originally Posted by EddyTheDog (Post 19654725)
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.

BSleazy 06-04-2013 02:01 PM

You should be able to do something like this

Code:

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


All times are GMT -7. The time now is 07:12 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc