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.
I think you want to be merging it with the original query. No need to specify the category then, and works if it's using a custom taxonomy this way too.
$args = array(
'posts_per_page' => -1,
'orderby' => 'rand'
);
query_posts( array_merge( $wp_query->query, $args) );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.