Try adding the caller_get_posts parameter so that sticky posts are not handled separately:
$args=array(
'showposts' => 3,
'post__in' => $sticky,
'orderby'=> rand,
'category__in'=>array($cat),
'caller_get_posts' => 1
);
Thanks – tried that but it doesn’t seem to have any effect. They still seem to show by date and not randomly.
Can you post what is in the SQL query? That might help to see what is going on. Just copy and post the SQL output from this:
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
global $wp_query;
echo '<p>';print_r($wp_query->request);echo '</p>';
Apologies, but where do I put the code that you’ve mentioned?
This is the same $my_query that you showed in your first sample code above. I just added the two lines after if( $my_query->have_posts() ) {
Change this:
<?php
$cat = get_query_var('cat');
$sticky=get_option('sticky_posts');
$args=array(
'showposts' => 3,
'post__in' => $sticky,
'orderby'=> rand,
'category__in'=>array($cat)
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
to this:
<?php
$cat = get_query_var('cat');
$sticky=get_option('sticky_posts');
$args=array(
'showposts' => 3,
'post__in' => $sticky,
'orderby'=> rand,
'category__in'=>array($cat)
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
global $wp_query;
echo '<p>';print_r($wp_query->request);echo '</p>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
It will print out the SQL from the query ahead of your posts. Just copy from the screen and paste it back here.
Ah, sorry – it’s getting late and my brain is a little fried!
Here’s what it outputted:
SELECT SQL_CALC_FOUND_ROWS distinct mYh7Tf_posts.* FROM mYh7Tf_posts INNER JOIN mYh7Tf_term_relationships ON (mYh7Tf_posts.ID = mYh7Tf_term_relationships.object_id) INNER JOIN mYh7Tf_term_taxonomy ON (mYh7Tf_term_relationships.term_taxonomy_id = mYh7Tf_term_taxonomy.term_taxonomy_id) WHERE 1=1 AND mYh7Tf_term_taxonomy.taxonomy = ‘category’ AND mYh7Tf_term_taxonomy.term_id IN (‘4′, ’25’, ’26’, ’27’, ’28’, ’29’, ’30’, ’31’, ’32’) AND mYh7Tf_posts.post_type = ‘post’ AND (mYh7Tf_posts.post_status = ‘publish’ OR mYh7Tf_posts.post_status = ‘private’) GROUP BY mYh7Tf_posts.ID ORDER BY mYh7Tf_posts.post_date DESC LIMIT 0, 10
Alright, if you look at that code you can see that the ORDER BY clause is on post_date. The ‘rand’ is being ignored for some reason.
It may work to force the use of rand() by inserting lines at the first of the code you posted, like this:
Change this:
<?php
$cat = get_query_var('cat');
to this:
<?php
function mam_posts_orderby ($orderby) {
global $mam_global_orderby;
if ($mam_global_orderby) $orderby = $mam_global_orderby;
return $orderby;
}
add_filter('posts_orderby','mam_posts_orderby');
global $mam_global_orderby;
$mam_global_orderby = 'rand()';
$cat = get_query_var('cat');
You can remove the two lines you put in to print out the SQL.
Spot on. Thank you so much for all your help with this. Really appreciated.
Glad it worked for you. Now, please use the dropdown at top right to mark this topic ‘Resolved’.