对于附件帖子,应执行以下操作,即使用wp_insert_attachment_data
hook 随着get_post()
:
function my_random_post_id( $data, $postarr ) {
// Runs if the post is being created and not updated.
if ( empty( $postarr[\'ID\'] ) ) {
// Locate a yet-unused ID in the (wp_)posts table.
do {
// Based on the dfx_random_user_id_get_max_id() function.
$max_post_id = ( ( 9007199254740991 + 1 ) / 2 ) - 1;
$ID = random_int( 1, $max_post_id );
} while ( get_post( $ID ) );
$data += compact( \'ID\' );
}
return $data;
}
add_filter( \'wp_insert_attachment_data\', \'my_random_post_id\', 10, 2 );
// The following is for non attachments.
//add_filter( \'wp_insert_post_data\', \'my_random_post_id\', 10, 2 );