我需要帮助。提前谢谢。我需要一个功能,更新后的标题和一些默认的标题加上当前日期后,职位状态是从草稿更新为发布slug。
我用这个来更新状态更改后的发布日期,但我需要添加一个功能,标题也要更新(使用默认标题),加上当前日期也要显示在更新的标题中。
//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;
}
示例:
J、 Doe注册到我的网站并发布新帖子。
他给这篇文章起了个疯狂的名字。
J、 Doe的帖子转到草稿。
两周后,Draft scheduler插件从drafts中获取J.Doe的帖子,并通过将其状态更改为published来发布。
此时,所需的函数必须能够用当前日期更新发布日期,用默认日期重命名J.Doe的疯狂标题,将当前日期添加到标题中,并更新slug。
SO网友:Grandeto
我想我找到了解决方案:
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;
}
SO网友:Mohamed Rihan
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;
}