0

Let's say I have this URL: www.example.com/myCPT/post, here I want to check if 3 days has passed since the post was created to redirect to www.example.com/myCPT/post/stats.

In this 3 days time frame the user should not be able to access www.example.com/myCPT/post/stats.

But this needs to be dynamic, every time a post is created to check for it's URL and add a 3 days time till this URL will be accessible www.example.com/myCPT/post/stats

For example I will have post1, post2, post3 and so on and every time the post will be created to add a 3 days time frame till the "/post/stats" will be available.

Also I need the same for "/post/comments" : this address should be available after 3 days, and if someone try to access before should be redirected to post url: "/post"

I did some research and I found this:

  • wp_redirect( $location, $status ); for redirection
  • <?php echo get_the_date(); ?> and <?php echo get_the_time(); ?> for gettin the date/time of the post

Found a snippet which might be helpful :

if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( 30 * 24 * 60 * 60 ) ) {
    // DO SOMETHING 
}
return $posts;
}

Later Edit :

The "/stats" and "/comments" endpoints are built like this :

function wpa121567_rewrite_endpoints(){

  add_rewrite_endpoint( 'stats', EP_PERMALINK );

}

add_action( 'init', 'wpa121567_rewrite_endpoints' );

Any suggestions on how can I achieve this?

  • "/stats" it's the same post but with an addition variable, and yes the url is created via an endpoint. I've updated my question with how "/stats" is created – agis Nov 27 '13 at 18:28
1

To redirect after 3 days gone after publication, hook into template_redirect, check if is a singular cpt view, check the date and compare to current time and redirect if needed.

In the 3 days time frame, check if stats is is the query vars and if so redirect to post page.

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();
    }
  }
}
| improve this answer | |
  • Just tried it, but is not working, what I mean by this is that when I access www.mywebsite.com/cpt/post it loads very very slow and after I get this: This webpage has a redirect loop and the URL in the browser is looking like this : www.mywebsite.com/cpt/post//stats. This should not redirect me to "/stats" it should be the other way around when I'm trying to access "/stats" to redirect me to "/post". – agis Nov 27 '13 at 19:07
  • Now I'm not getting the error anymore, but it still redirects me to "/stats" and it should redirect me after 3 days. Also when I'm trying to access "/stats" before 3 days has passed it should redirect me to the "/post" – agis Nov 27 '13 at 19:34
  • I've did this, but still when I'm accessing /post it redirects me /stats and I want to be redirected to /stats if 3 days has passed. – agis Nov 27 '13 at 19:48
  • 1
    P.S. for "3 days has gone" I intend that if the post was published on day 10, it redirects to '/stats' starting form day 14. @Alecs – gmazzap Nov 27 '13 at 19:56
  • Yes, You're right ! my mistake, the post was older than 3 days. Thank you very much! And one more question, is it possible to add another variable to check for ? because I want to check also for another endpoint called "/comments" so that after 3 days user can access the "/comments". – agis Nov 27 '13 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.