Skip to content

Instantly share code, notes, and snippets.

@mypacecreator
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save mypacecreator/9406423 to your computer and use it in GitHub Desktop.

Select an option

Save mypacecreator/9406423 to your computer and use it in GitHub Desktop.
特定の条件下で投稿の表示件数を変えたりするアクションフックpre_get_postsのスニペット
<?php
function customed_queries ( $query ) {
if ( is_feed() ) { // フィードにカスタム投稿を含める
$query->set( 'post_type', array( 'post', 'gallery' ) );
return $query;
}
if ( is_admin() || ! $query->is_main_query() ) { //管理画面とサブクエリは対象外
return;
}
if ( $query->is_main_query() ) {
if ( is_post_type_archive('gallery') ) {
$query->set( 'posts_per_page', 30 ); // ギャラリーのときは30件表示
}
}
}
add_action( 'pre_get_posts', 'customed_queries' );
@mypacecreator
Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment