I cannot get my Wordpress theme to randomise the posts I'm displaying in the category archives [I'm using it as a CMS]. The homepage randomises normally, and I am [I think] correctly altering the WP_query. Below is the exact args array:
array(4) { ["orderby"]=> string(4) "rand" ["order"]=> string(3) "ASC" ["posts_per_page"]=> string(2) "-1" ["category_name"]=> string(8) "branding" }
For easier reading it is:
orderby => rand
order => ASC
posts_per_page => -1
category_name => branding (or whatever the query_string brings in)
I get all the posts from the category, but they are in post date order.
Any clues? or alternate methods for shuffling the result of my WP_query in the have_posts?
Thanks.
************EDIT************
Sorry I should have been more clear about the args array above. It is a var_dump of the query array, not my arguments I am adding to the query.
$args = array(
'orderby' => 'rand',
'order' => 'ASC',
'posts_per_page' => '-1',
);
global $wp_query;
remove_all_filters('posts_orderby');
$theq = array_merge($args, $wp_query->query);
query_posts($theq);
I added the remove_all_filters as per Sheikh Heera suggestion, but it hasn't made a difference.
You might be better off creating a new query then. This should only be used on an taxonomy template though like category.php or taxonomy-yourcustomtaxonomy.php.
global $wp_query;
$term = $wp_query->queried_object;
$args=array(
'orderby' => 'rand',
'posts_per_page' => -1,
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'field' => 'slug',
'terms' => $term->slug,
)
)
);
$new_query = null;
$new_query = new WP_Query($args);
while ($new_query->have_posts()) : $new_query->the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div class="entry-meta"><?php // Meta ?></div><!-- .entry-meta -->
<div class="entry-content"><?php the_content(); ?></div>
</div>
<?php
endwhile;
wp_reset_postdata();
It could be another plugin that creating the problem but you can do as follows
remove_all_filters('posts_orderby');
$args=array(
orderby => 'rand'
order => 'ASC'
posts_per_page => -1
category_name => 'branding'
);
query_posts($args);
But remember, you can wreck a plugin's functionality with this solution, but it could be useful to solve the problem but may be not perfect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With