0

I have a multi-user blog, and would like to implement two functions in WordPress 4.3.1. The first function moves publish posts in pending after 30 days. I used a add_action. My code:

function expire_posts() {
    global $wpdb;
    $daystogo = "1";
    $sql =
    "UPDATE {$wpdb->posts}
    SET post_status = 'pending'
    WHERE (post_type = 'post' AND post_status = 'publish')
    AND DATEDIFF(NOW(), post_date) > %d";

    $wpdb->query( $wpdb->prepare( $sql, $daystogo ) );
}
add_action('wp_head', 'expire_posts');
add_action( 'admin_head', 'expire_posts',98 );

Code to auto expire posts after 30 days

The second function, update the post date, when users update the posts. I used a add_filter. My code:

function reset_post_date_wpse_121565($data,$postarr) {
    global $post;
    $ageunix = get_the_time('U');
    $days_old_in_seconds = ((time() - $ageunix));
    $days_old = (($days_old_in_seconds/86400));
    if ($days_old > 1) {
      // var_dump($data,$postarr); die; // debug
      $data['post_date'] = $data['post_modified'];
      $data['post_date_gmt'] = $data['post_modified_gmt'];
      return $data;
    }
}

add_filter('wp_insert_post_data','reset_post_date_wpse_121565',97,2);

Update post date to modified date automatically

Together in the function.php:

function expire_posts() {
    global $wpdb;
    $daystogo = "1";
    $sql =
    "UPDATE {$wpdb->posts}
    SET post_status = 'pending'
    WHERE (post_type = 'post' AND post_status = 'publish')
    AND DATEDIFF(NOW(), post_date) > %d";
    $wpdb->query( $wpdb->prepare( $sql, $daystogo ) );

}
add_action('wp_head', 'expire_posts');
add_action( 'admin_head', 'expire_posts',98 );

function reset_post_date_wpse_121565($data,$postarr) {

    global $post;
    $ageunix = get_the_time('U');
    $days_old_in_seconds = ((time() - $ageunix));
    $days_old = (($days_old_in_seconds/86400));
    if ($days_old > 1) {
        // var_dump($data,$postarr); die; // debug
        $data['post_date'] = $data['post_modified'];
        $data['post_date_gmt'] = $data['post_modified_gmt'];
        return $data;
    }
}
add_filter('wp_insert_post_data','reset_post_date_wpse_121565',97,2);

Separately they work and work for me, but not work together. I'm pretty sure that create a conflict with the loading of the page admin, but I do not know how to fix it, that is, make them work together.

I'm going crazy! Some idea?

Thank you!

Ok, with wp_cron, the first function moves publish posts in pending after 30 days. Much better! My code:


if ( ! wp_next_scheduled( 'expire_posts_hook' ) ) {
  wp_schedule_event( time(), 'hourly', 'expire_posts_hook' );
}
add_action( 'expire_posts_hook', 'expire_posts' );
function expire_posts() {
global $wpdb;
$daystogo = "1";
$sql =
"UPDATE {$wpdb->posts}
SET post_status = 'pending'
WHERE (post_type = 'post' AND post_status = 'publish')
AND DATEDIFF(NOW(), post_date) > %d";
$wpdb->query( $wpdb->prepare( $sql, $daystogo ) );}

Now, I need the second function, update the post date, when users update the posts, BUT ONLY WHEN THE POST IS NOT PUBLISHED (for example in pending), is possible???

My code work for all status:


function reset_post_date($data,$postarr) {
  // var_dump($data,$postarr); die; // debug
  $data['post_date'] = $data['post_modified'];
  $data['post_date_gmt'] = $data['post_modified_gmt'];
  return $data;
}
add_filter('wp_insert_post_data','reset_post_date',99,2);

I need the working ONLY WHEN THE POST IS NOT PUBLISHED.

Help me please, Thanks!!

  • You should pass the post or post ID in as an argument to your functions, that way you know exactly which post you're operating on, rather than assuming the current set post is what you're working on and making accidents. Also, doing SQL queries to update posts doesn't fire hooks that might be important, wp_update_post would be better – Tom J Nowell Nov 26 '15 at 18:35
  • Also, why does reset_post_date_wpse_121565 not return something if it's less than a day old? – Tom J Nowell Nov 26 '15 at 18:40
  • 1
    Your expire_posts() runs on every page load. Why aren't you using wp_cron? This is very, very profligate code. – s_ha_dum Nov 26 '15 at 19:07
  • Thanks, you're right, I wrote 30 days in the title, but to prove quickly, i entered one day, in the function. I'll try with wp_cron for expire_posts() and let you know, thanks !! – Claudio Nov 26 '15 at 21:10
  • @Claudio How was this? I'm trying to do the same. – nicogaldo Jul 22 '16 at 12:53

Your Answer

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

Browse other questions tagged or ask your own question.