0

I need help. Thanks in advance. I need a function that update post title and slug with some default title + current date when post status is updated from draft to published.

I use this one to update the post date on status change but I need to add a functionality the title also to be updated (with a default one) plus current date also to be shown in the updated title.

//auto change post date on every status change

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

Example:

J. Doe register to my site and publish a new post.

He gives the post some crazy name.

J. Doe's post go to drafts.

After 2 weeks Draft scheduler Plugin takes J. Doe's post from drafts and publish it by changing its status to published.

At this point the desired function has to be able to update the post date with the current date, rename J. Doe's crazy title with a default one, add the current date into the title and update the slug.

0

2 Answers 2

1

I think I found the solution:

 add_filter('wp_insert_post_data','reset_post_date',99,2);

     function reset_post_date($data,$postarr) {

     //update post time on status change
     $data['post_date'] = $data['post_modified'];
     $data['post_date_gmt'] = $data['post_modified_gmt'];

     //also update title and add the current date to title
     $data['post_title'] = 'Your Default Title - '. current_time ( 'm-d-Y' );

     //also update the slug of the post for the URL
     $data['post_name'] = wp_unique_post_slug( sanitize_title( $data['post_title'] ), $postarr['ID'], $data['post_status'], $data['post_type'], $data['post_parent'] );

     return $data;  
 }
2
  • but be careful. this function also will automatically rename on update your pages and even your menus! I don't know why the menus are also affected. Anybody who knows?
    – Grandeto
    Commented Mar 9, 2016 at 11:37
  • now I need to place the category name instead of 'Your Default Title - ' ??? May you help me with that
    – Grandeto
    Commented Mar 9, 2016 at 13:15
0

maybe try this.

add_filter('wp_insert_post_data','reset_post_date',99,2);

     function reset_post_date($data,$postarr) {

     //update post time on status change
     $data['post_date'] = $data['post_modified'];
     $data['post_date_gmt'] = $data['post_modified_gmt'];

     //also update title and add the current date to title
    if( is_tax() ) {

        global $wp_query;
        $term = $wp_query->get_queried_object();
        $cat_name = $term->name;

        $data['post_title'] = $cat_name .' '. current_time ( 'm-d-Y' );
    }else{ 
        $data['post_title'] = 'name'. current_time ( 'm-d-Y' );
    }
     //also update the slug of the post for the URL
     $data['post_name'] = wp_unique_post_slug( sanitize_title( $data['post_title'] ), $postarr['ID'], $data['post_status'], $data['post_type'], $data['post_parent'] );

     return $data;  
 }
1
  • nope... it returns this kind of title everytime: 'name 03-12-2016'
    – Grandeto
    Commented Mar 12, 2016 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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