php - How can I make so that the order of this loop's output is random? -
in wordpress site, taxonomy-product-category.php template has following loop loading posts page:
<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> however, want posts randomly ordered rather following specific order date added.
how can modify loop it?
you can use pre_get_posts set random ordering taxonomy pages. note, random ordering duplicates posts between paged pages each page new query , not extension one. unfortunately how random ordering works.
you can try following
add_action( 'pre_get_posts', function ( $q ) { if ( !is_admin() // targets front end && $q->is_main_query() // targets main query && $q->is_tax( 'product-category' ) // targets product-category tax pages ) { $q->set( 'orderby', 'rand' ); } });
Comments
Post a Comment