function add_user_rewrite_rules()
{
add_rewrite_rule("^post_type1/?$", "index.php?post_type=post_type1&mode=archive", 'top');
add_rewrite_rule("^post_type1/article/([^\/]+)/?$", 'index.php?post_type=post_type1&name=$matches[1]&mode=single', 'top');
add_rewrite_rule("^post_type2/?$", "index.php?post_type=post_type2&mode=archive", 'top');
add_rewrite_rule("^post_type2/(\d+)/?$", 'index.php?post_type=post_type2&p=$matches[1]&mode=single', 'top');
}
add_action('init', 'add_user_rewrite_rules');
function add_user_routes_query_vars($query_vars)
{
$query_vars[] = 'mode';
$query_vars[] = 'param';
return $query_vars;
}
add_filter('query_vars', 'add_user_routes_query_vars');
/**
* is_single,is_archiveの返値が確定する前に処理
* @param $query
*/
function my_parse_request($query)
{
if(is_admin())
return;
if($query->query_vars['post_type'] === 'post_type1') {
$query->query_vars['posts_per_page'] = 10;
}else if($query->query_vars['post_type'] === 'post_type2') {
$query->query_vars['posts_per_page'] = 20;
}else if($query->query_vars['post_type'] === 'post_type3'){
}
}
add_action( 'parse_request', 'my_parse_request');
function my_pre_get_posts( $query )
{
if ( is_admin() || ! $query->is_main_query() ){
return;
}
if($query->query['post_type'] === 'post_type1') {
if (!empty($_GET['api'])) {
if(!empty($_GET['count']) && preg_match('/^\d+$/',$_GET['count'])){
$query->set('posts_per_page',$_GET['count']);
}else{
$query->set('posts_per_page',3);
}
}
}else if($query->query['post_type'] === 'post_type2') {
if (!empty($_GET['api'])) {
if (isset($_GET['cate'])) {
$tax_query[] = array(
'taxonomy' => 'tax_type',
'terms' => array($_GET['cate']),
'field' => 'slug',
'operator' => 'IN');
$query->set('tax_query', array('relation' => 'OR', $tax_query));
}
if(!empty($_GET['count']) && preg_match('/^\d+$/',$_GET['count'])){
$query->set('posts_per_page',$_GET['count']);
}else{
$query->set('posts_per_page',3);
}
}else if (isset($query->query['param'])) {
$tax_query[] = array(
'taxonomy' => 'tax_type',
'terms' => array($query->query['param']),
'field' => 'slug',
'operator' => 'IN');
$query->set('tax_query', array('relation' => 'OR', $tax_query));
}
}
}
add_action('pre_get_posts','my_pre_get_posts');
/**
* テンプレートファイル切替
* @param $template
* @return mixed
*/
function my_template_include($template)
{
global $wp_query;
if($wp_query->query['post_type'] === 'post_type1' && isset($wp_query->query['mode']) && $wp_query->query['mode'] === 'top') {
$bn = basename($template);
return preg_replace("/{$bn}$/","index-post_type1.php",$template);
}else if($wp_query->query['post_type'] === 'post_type2' && isset($wp_query->query['mode']) && $wp_query->query['mode'] === 'top'){
$bn = basename($template);
return preg_replace("/{$bn}$/","archive-post_type2.php",$template);
}else if($wp_query->query['post_type'] === 'post_type3'){
if($wp_query->query['mode'] === 'single'){
$bn = basename($template);
return preg_replace("/{$bn}$/","single-post_type3.php",$template);
}
}
return $template;
}
add_filter('template_include','my_template_include');