我扩展了@brasofilo的上述解决方案,也将附件发布日期更改为发布家长的日期。
不仅在上传附件时,而且在编辑附件时。您可以使用edit_attachment
此过滤器具有相同的功能。然而,当您这样做时wp_update_post
函数会导致无限循环并导致PHP内存错误,经过多次搜索,我找到了解决方法。实际上,在Codex.
解决方案是移除过滤器,如下所示:
function wpse_55801_attachment_author( $attachment_ID ) {
remove_filter(\'add_attachment\', \'wpse_55801_attachment_author\');
remove_filter(\'edit_attachment\', \'wpse_55801_attachment_author\');
$attach = get_post( $attachment_ID );
$parent = get_post( $attach->post_parent );
$the_post = array();
$the_post[\'ID\'] = $attachment_ID;
$the_post[\'post_author\'] = $parent->post_author;
$the_post[\'post_date\'] = $parent->post_date;
wp_update_post( $the_post );
add_filter( \'add_attachment\', \'wpse_55801_attachment_author\' );
add_filter( \'edit_attachment\', \'wpse_55801_attachment_author\' );
}
add_filter( \'add_attachment\', \'wpse_55801_attachment_author\' );
add_filter( \'edit_attachment\', \'wpse_55801_attachment_author\' );