wp_update_post()
最终使用wp_insert_post()
, 其中wp_insert_post_data
在里面。过滤器“在将post数据插入数据库之前对其进行了筛选。”我认为这是添加您描述的自定义逻辑的合适时机publishorpend
方法中似乎没有其他可用的筛选器。
你也许可以这样做,
add_filter(\'wp_insert_post_data\', \'filter_wp_insert_post_data\', 10, 2);
function filter_wp_insert_post_data( $data, $postarr ) {
// Only affect custom post type. Update the post type slug!
if ( \'my_post_type\' !== get_post_type($data[\'ID\']) ) {
return $data;
}
// You may want to have some extra checks so that the status change doesn\'t affect every post of this type
// if ( ! some_theme_or_plugin_function_to_check_if_orphaned($data[\'ID\']) ) {
// return $data;
// }
// assuming the function returns boolean or truthy/falsy value
$requires_approval = mylisting_get_setting( \'submission_requires_approval\' );
// If submission_requires_approval is false, post_status should be set to publish no matter what the user role is (this is how the function works currently).
if ( ! $requires_approval ) {
return $data;
}
// if current user is a member of administrators or editors, the post status should be set to publish. note, the current user may have more than one role assigned.
// As a personal preference, check capabilities instead of role. Admins and editors can edit_others_posts
if ( current_user_can( \'edit_others_posts\' ) ) {
$data[\'post_status\'] = \'publish\';
// If submission_requires_approval is true and if the current user is a group other than admin/editor (or is not logged in at all*) post_status should be set to pending.
} else if ( $requires_approval ) {
$data[\'post_status\'] = \'pending\';
}
return $data;
}
这只是一个示例,可能需要一些调整来匹配您的确切设置和需要。