这里有一种方法可以为新帖子添加一个已经添加并可见的自定义字段:
/**
* Preset a custom field for new posts
*
* @link http://wordpress.stackexchange.com/a/200554/26350
*/
add_action( \'save_post_post\', function( $post_ID, $post, $update )
{
if(
is_a( $post, \'\\WP_Post\' )
&& \'auto-draft\' === $post->post_status
&& post_type_supports( $post->post_type, \'custom-fields\' )
&& \'0000-00-00 00:00:00\' === $post->post_date_gmt
&& $post_ID > 0
&& ! $update
)
add_post_meta( $post_ID, \'wpse_custom_field\', \'123\' );
}, 10, 3 );
这里我们使用
save_post_{post-type}
钩
然后,我们将在添加新帖子屏幕上看到:
正如@Alpha\\u Hydrae和@MarkKaplen的评论所述,我们应该能够将其简化为:
/**
* Preset a custom field for new posts
*
* @link http://wordpress.stackexchange.com/a/200554/26350
*/
add_action( \'save_post_post\', function( $post_ID )
{
if( \'auto-draft\' === get_post_status( $post_ID )
&& post_type_supports( get_post_type( $post_ID ), \'custom-fields\' )
)
add_post_meta( $post_ID, \'wpse_custom_field\', \'123\' );
} );