I have a Custom Post Type called "Report." It has some custom fields, including the name of the place where the post has been written.

I would like that when a user saves (or updates) the post from the admin interface, if he did not fill the place name, the system will fill in automatically on the basis of latitude/longitude (using reverse geocoding).

Which hook should I use?


You would use "save_post" hook. It will look something like this.

add_action( 'save_post', 'populate_empty_place' );

function populate_empty_place( $post_id ) {

    //verify post is not a revision
    if ( !wp_is_post_revision( $post_id )  && get_post_type($post_id) == 'report') {

        // Check for empty place
        // And popluate with your own value     

  • That's exactly what I thought in the first place. Unfortunately, if I insert into the if the code update_post_meta($post_id, 'nearby_place_name', 'New York');, when the user tries to add a new report post (before saving it) the place name is set to New York. – User Dec 13 '12 at 13:12
  • you mean it replaces the user's added place? – M-R Dec 13 '12 at 13:18
  • No, as soon as the user clicks on "Add Report", so before he entered any data, the place name is set to New York. – User Dec 13 '12 at 13:22
  • Please update your question and particularly specify that you want to populate when add post is clicked not after the post is saved/updated. Is this field displayed through a metabox? – M-R Dec 13 '12 at 13:40
  • No, like I said in the original post, I want to populate the place when the user saves (or updates) the post. But with your solution, the place is populated before that, i.e. when the user clicks on "Add Report" (that is not what I want). Btw, yes, this field is displayed through a metabox. – User Dec 13 '12 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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