実現したいこと
wordpressのカスタム投稿一覧で記事をランダム表示をさせてページングにも対応させたいと考えています。
どなたかご教示いただけないでしょうか?
発生している問題
現状1ページに16件の記事一覧がランダムに表示するようにしています。
しかし、2ページ目以降に移動すると前のページで表示されていた記事が重複して表示されます。
2ページ目以降も記事が重複して出ないようにしたいのですが・・・
taxonomy-shopcategory-foodshop.php ソースコード <?php $args = array( 'posts_per_page' => '16', 'post_type' => 'shop', 'post_status' => 'publish', 'orderby' => 'rand', 'paged' => $paged, 'tax_query' => array( array( 'taxonomy' => 'shopcategory', 'field' => 'slug', 'terms' => 'foodshop' ) ) ); $customPosts = get_posts($args); if ($customPosts) : foreach ($customPosts as $post) : setup_postdata($post); $terms = get_the_terms($post->ID, 'category_name'); foreach ($terms as $term) { $term_slug = $term->slug; }?>
試したこと
下記のサイトを参考にfanctions.phpに記述しましたが、動作していないようです。
http://designhack.slashlab.net/
function category_rand_orderby( $orderby, $query ){ if ( !is_admin() && $query->is_main_query() ) { if ( $query->is_tax() ) { $seed = strtotime( date( 'Y-m-d H:00:00' ) ); mt_srand( $seed ); $orderby = 'RAND(' . mt_rand() . ')'; } } remove_filter( current_filter(), __FUNCTION__ ); return $orderby; } add_filter( 'posts_orderby', 'category_rand_orderby', 10, 2 );