WP_TRASH_POST函数只适用于帖子,不适用于页面!

时间:2014-03-05 作者:dkl

我有以下函数,可以在删除帖子时进行扣减。但是当我删除一个页面时,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\');
是否有办法限制此功能仅适用于帖子而不适用于页面?

1 个回复
最合适的回答,由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 是一个帖子。

结束

相关推荐

WP_Query in functions.php

我有一些代码要转换成函数。它工作得很好,直到我将其包装到所述函数中: $args = array( \'posts_per_page\' => -1, \'post_type\' => \'asset\', \'category_name\' => $cat ); $cat_query = new WP_Query( $args );