• Resolved jakemc

    (@jakemc)


    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!

Viewing 4 replies - 1 through 4 (of 4 total)
  • I notice that your plugin hook is called three times, for the three different functions edit_, save_ and publish_post. That might be why it is added three times.

    The assumption there is that all three functions are called when a post is modified.

    One other thing to note, for security reasons it’s always best to choose either $_GET or $_POST (or $_COOKIE, if you’re using that) instead of $_REQUEST.

    Thread Starter jakemc

    (@jakemc)

    Aha! I hunted down the hooks (in functions-post.php) and save_post is the catch all – the other two are called under special conditions (e.g., edit_post when another variable $update has been set) and so may be called *as well* as save_post.

    Many thanks again, Maerk.

    Yeah, edit_post, I would presume, is for when you … well … edit a post 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Problem adding custom meta data’ is closed to new replies.