我想在发布帖子时更改帖子的日期。我写在我的插件
add_action( 'publish_post', 'myPlugin_published' );
function myPlugin_published()
{
$postdate = '2010-02-23 18:57:33';/*For example*/
$new_post = array(
'post_date' => $postdate
);
wp_insert_post($new_post);
}
Run Code Online (Sandbox Code Playgroud)
问题是日期没有改变。
如何在 WordPress 中更改帖子的日期?
谢谢大家的帮助
function my_function( $post_id )
{
$postdate = '2010-02-23 18:57:33';
$my_args = array(
'ID' => $post_id,
'post_date' => $postdate
);
if ( ! wp_is_post_revision( $post_id ) ){
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'my_function');
// update the post, which calls save_post again
wp_update_post( $my_args );
// re-hook this function
add_action('save_post', 'my_function');
}
}
add_action('save_post', 'my_function');
Run Code Online (Sandbox Code Playgroud)