wp_trash_post function to only apply to posts, not pages!

I have the following function that deducts at point when a post is removed. But when I remove a page, the wp_trash_post is also called.

// 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');

Is there a way to restrict this function to only apply to posts and not pages?

Related posts

1 comment

  1. You could just add something like:

    // 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');
    

    Then your point subtraction will only occur if the $post is a post.

Comments are closed.