check if wordpress is updating or publishing a post

Very simple question: How can I check when wp is doing an update or a publishing to a post? Because I have to check a postmeta value which could be only true if the post is actually being published and so it has not to be already present in the system.
E.g. if( defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE ) it’s defined when wp is doing an autosave. Is there anything like this when it’s doing an update?
Thank you!!

ok so

Read More
add_action('save_post', 'fields_news_save');
function fields_news_save($post_id){
$errors = false;
       if(get_check_key($_POST['checks_news'])==0){
    $errors = true;     
    update_option('custom_token', $errors);
    update_option('custom_admin_errors', $txt_err=error_text($errors, 2));

    remove_action('save_post','fields_news_save');
    wp_update_post(array('ID' => $post_id, 'post_status' => 'draft'));
    add_action('save_post', 'fields_news_save');

    return false;
}
}

The “if” clause check if there are meta post values set as $_POST[‘checks_news’] and this is the function which does the quer:
function get_check_key($checker){
global $wpdb;
$data = array();
$wpdb->query("
SELECT *
FROM $wpdb->wp_postmeta
WHERE
meta_value= $checker;
");
return $wpdb->num_rows;
}

Now what i would like to do is the “if” at the beginnig checks if the number of rows is 0 AND wp is doing a publish not an update.

Related posts

Leave a Reply

2 comments

  1. I avoid the problem just checking if the ID of the post is already existing in the db.

    get_post($id)==NULL
    

    That’s probably the easiest way to do what I need.

  2. If you need to perform an action when a “publish post”, “save post”, or “draft post” action is taking place, then simply add a callback to the appropriate action hook, i.e. publish_post, save_post, or draft_post.

    If you need to perform an action during the transition of a post from one status to another, then you may want to use the appropriate post status transition hook.