Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

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 WHEREmeta_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.

share|improve this question
    
Could you post some code of where you are saving the postmeta. Do you mean you that only want to add the postmeta if the post is being published, and not add if it already is published..? –  Stephen Harris Feb 22 '12 at 14:47
    
Hi Stephen, It's nothing about metadata, I only need to know in the backend (when I press the button publish/update) if it has been a publishing of a new post or an updating of an existing post. –  Tony Feb 22 '12 at 15:22
1  
@Tony, this is built in functionality. If a post has been published, the button will read "Update". If it hasn't been published, it will read "Publish". –  Brian Fegter Feb 22 '12 at 15:55
    
Mind me asking why? If I know what you're trying to do, It'll be easier to help :D. –  Stephen Harris Feb 22 '12 at 15:56
    
Is the save_post action something that would work for you? It's fired whenever wp_insert_post is called and works on auto drafts. It would definitely help to know what you're trying to do! –  Kyle Feb 22 '12 at 16:12
show 4 more comments

2 Answers

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.

share|improve this answer
add comment
up vote 0 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.