Wordpress - random post order...

Associate
Joined
21 Oct 2008
Posts
1,679
Location
Mooching... in your house
hi,

was just wondering if anyone knows how to set the order of posts displayed on a page to be random?

i'm not talking about a bespoke query (something like <?php $news = new WP_Query("cat=15&showposts=2"); ?>) as i assume that would be as simple as adding orderby=rand to the arguments? i'm talking about a page where there is a standard loop...

<?php while (have_posts()) : the_post(); ?>

or whatever... can i make the post order of something like this random?
 
naah i did see that one and a few other plugins but none seem to fit the bill... i just want the loop to work as normal only in a random order...

maybe i've gotta code it straight using mysql and php?
 
ok, so i think there is a gap in my knowledge which when filled I can maybe workout the solution...

if you want to create a custom $post object, you can use:

PHP:
<?php $news = new WP_Query("cat=15&showposts=2"); ?>

fine i understand all this, but if you are using a standard loop:

PHP:
<?php while (have_posts()) : the_post(); ?>

what and where is the WP_Query bit?

something obviously has to still query the database but instead of querying based on what you've told it to it will query based on the page you are on...

so is there an underlying bit of code somewhere that simply runs a WP_Query and inputs the relevant cat=x parameter?

my dilemma is that i can use a random query perfectly fine, i used this:

PHP:
$rand_posts = get_posts('cat=4&numberposts=10&orderby=rand');
foreach( $rand_posts as $post ) :

// ...

endforeach;

but if i do it this way the content does not change based on the page i am on, it is always just a random 10 posts from category 4... if i'm on say a category 8 page, i want 10 random posts from category 8 and that isn't possible... do you see the problem? (finding hard to explain) - if anyone could give me a leg up i'd much appreciate it...
 
What page your template are using this code in?

WordPress can use different template files for displaying your blog in different ways. In the default WordPress theme, there are template files for the index view, category view, and archive view, as well as a template for viewing individual posts.

Depending on which one you're viewing will depend on how it handles the while loop.

So are you using an existing one or using your own custom page?

I would imagine it's a matter of randomising the ID that's, but i'm not if WP_Query grabs the ID or not..
 
Back
Top Bottom