I need to do this in my post in custom post type, I need when to day publish new post. After 1 year from today convert post status from publish to draft.

How can do that ?

  • 1
    This is very possible, and I believe that this has been answered before. What have you actually tried yourself and where did you fail. It is much easier to help you if you can share your code and explain where you have failed please file an edit and add all relevant info inside your post Aug 30 '15 at 8:35
  • i dont have any code for this so i am here to get steps to help
    – Android-wd
    Aug 30 '15 at 9:20

Try this code (maybe working) , but i'am not sure cause not testing :

function amin_show_expire(){
  global $wpdb;
  $result = $wpdb->get_results("SELECT * FROM wp_posts WHERE post_type = 'CUSTOM' AND post_status = 'publish'");
  if( !empty($result)) foreach ($result as $a){
      $show_time = get_the_time("Y-m-d", $a->ID );
      $show_time=date("Y-m-d", strtotime("+1 years", strtotime($show_time)));
      if ( $show_time < date("Y-m-d")){//compare 1 year after post with current time
          $my_post = array();
          $my_post['ID'] = $a->ID;
          $my_post['post_status'] = 'draft';
          wp_update_post( $my_post );
  } // end foreach

add_action( 'init', 'amin_show_expire' );
  • i add it in function.php and show me this error WordPress database error: [Table 'cartan.wp_posts' doesn't exist] SELECT * FROM wp_posts WHERE post_type = 'serve' AND post_status = 'publish'
    – Android-wd
    Aug 30 '15 at 15:27
  • @Android-wd -> Open phpmyadmin and see what posts table's name is ? for example rt_posts,wp_posts and etc. (depend to prefix name like wp,rt and so on) Aug 30 '15 at 17:37
  • That is a very expensive operation to run. Aug 30 '15 at 17:40

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.