要在发布3天后重定向,请连接到template_redirect
, 检查是否是单一cpt视图,检查日期并与当前时间进行比较,如果需要,则重定向。
在3天的时间范围内,检查stats是否是查询变量,如果是,则重定向到post页面。
add_action(\'template_redirect\', \'check_the_date_for_stats\');
function check_the_date_for_stats() {
if ( is_singular(\'myCPT \') ) { // adjust myCPT with your real cpt name
$pl = get_permalink( get_queried_object() ); // permalink
$is_stats = array_key_exists( \'stats\', $GLOBALS[\'wp_query\']->query ); // is stats?
$is_cm = array_key_exists( \'comments\', $GLOBALS[\'wp_query\']->query ); // is comments?
$ts = mysql2date(\'Ymd\', get_queried_object()->post_date_gmt ); // post day
$gone = ($ts + 3) < gmdate(\'Ymd\'); // more than 3 days gone?
if ( $gone && ( ! $is_stats && ! $is_cm ) ) {
// more than 3 days gone and not is stats => redirect to stats
wp_redirect( trailingslashit($pl) . \'/stats\' );
exit();
} elseif( ! $gone && ( $is_stats || $is_cm ) ) {
// we are in 3 days frame and trying to access to stats => redirect to post
wp_redirect( $pl );
exit();
}
}
}