Problem adding custom meta data
-
Hi.
I’m trying to create a plugin that will allow users to associate posts from a particular category with a post they’re editing by selecting from a dropdown. I want to do this by storing the id of the associated post as a meta value.
When I add a meta data association with the code I have, it is added 3 times. Can anyone tell me why and how to stop this? Here’s the code:
HTML:
<select name="professional_speciality">
<option value="#NONE#">-select-</option>
<option value="7">Endocrine</option>
<option value="4">Gastrointestinal</option>
<option value="3">General</option>
[... etc ...]
</select>
<input type="submit" name="save" value=" Save changes and add " />
The select options’ values are IDs of other posts.
My plugin has the following:
add_action( 'edit_post', 'jm_edit_meta_value' );
add_action( 'save_post', 'jm_edit_meta_value' );
add_action( 'publish_post', 'jm_edit_meta_value' );
function jm_edit_meta_value ($id) {
global $wpdb;
if( !isset( $id ) ) {
$id = $_REQUEST[ 'post_ID' ];
}
$meta_value = $_REQUEST[ "professional_speciality" ];
if( isset( $meta_value ) && !empty( $meta_value ) && ( $meta_value != '#NONE#' ) ) {
add_post_meta( $id, "professional_speciality", $meta_value );
}
}
Thanks in advance!
- The topic ‘Problem adding custom meta data’ is closed to new replies.