我有以下函数,可以在删除帖子时进行扣减。但是当我删除一个页面时,wp\\u trash\\u post也被称为。
// Remove 1 point if their post get removed
function deletePointFromUser($post_id) {
$post = get_post($post_id);
$authorid = $post->post_author;
$currentQPointNumber = get_user_meta($authorid, \'points\', true);
// Delete 1 to the current Point Score
update_user_meta($authorid, \'points\', $currentQPointNumber-1);
}
add_action(\'wp_trash_post\', \'deletePointFromUser\');
是否有办法限制此功能仅适用于帖子而不适用于页面?
最合适的回答,由SO网友:Douglas.Sesar 整理而成
您可以添加如下内容:
// Remove 1 point if their post get removed
function deletePointFromUser($post_id) {
$post = get_post($post_id);
if( $post->post_type != \'post\' ) return;//added code
$authorid = $post->post_author;
$currentQPointNumber = get_user_meta($authorid, \'points\', true);
// Delete 1 to the current Point Score
update_user_meta($authorid, \'points\', $currentQPointNumber-1);
}
add_action(\'wp_trash_post\', \'deletePointFromUser\');
那么只有当
$post
是一个帖子。