Skip to content

Instantly share code, notes, and snippets.

@kurudrive
Created May 28, 2020 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurudrive/92ce3be9024691c0bc70d54511fb9118 to your computer and use it in GitHub Desktop.
Save kurudrive/92ce3be9024691c0bc70d54511fb9118 to your computer and use it in GitHub Desktop.
カテゴリーアーカイブやタグアーカイブで表示順をランダムにする
function my_change_query( $query ) {
// 管理画面では何もしない
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
// カテゴリーページでランダムにする
if ( $query->is_category() ) {
$query->set( 'orderby', 'rand' );
return;
}
// タグページでランダムにする
if ( $query->is_tag() ) {
$query->set( 'orderby', 'rand' );
return;
}
}
add_action( 'pre_get_posts', 'my_change_query' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment