WordPress.org

Support

Support » Plugins and Hacks » Hacks » Custom meta saving not working!

Custom meta saving not working!

  • Hi all,

    Im stuck with the saving of custom meta for my custom post type.
    When the meta is filled with content it will save just fine but if I deleted the content of the meta it wont delete in the database/meta, so it can not be empty.

    Here is the code.

    add_action('save_post', 'save_details');
    function save_details($post_id){
    global $post;
    
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
    return $post_id;
    
    if (isset($_POST["subtitle"]) && $_POST["subtitle"] <> '') update_post_meta($post->ID, "subtitle", $_POST["subtitle"]);
    if (isset($_POST["youtube"]) && $_POST["youtube"] <> '') update_post_meta($post->ID, "youtube", $_POST["youtube"]);
    if (isset($_POST["call_in"]) && $_POST["call_in"] <> '') update_post_meta($post->ID, "call_in", $_POST["call_in"]);
    }

    Can does some one know how to fix this?

Viewing 4 replies - 1 through 4 (of 4 total)
  • update_post_meta never gets called if the content is empty due to your <> ” test.

    If you want to delete the meta data when there’s no content you’ll have to do something like,

    if (isset($_POST["subtitle"]) {
        if (empty($_POST["subtitle"])) {
            delete_post_data(...);
        } else {
            update_post_meta(...);
        }
    }

    Ian.

    Currently im using it like this

    if (isset($_POST["subtitle"]) {
        if (empty($_POST["subtitle"])) {
            delete_post_data($post->ID, "subtitle", $_POST["subtitle"]);
        } else {
            update_post_meta($post->ID, "subtitle", $_POST["subtitle"]);
        }
    }

    But it gives me an error : Parse error: syntax error, unexpected ‘{‘ on the line.

    How can I fix this?

    if (isset($_POST["subtitle"]) {
    replace it by ( because ‘if’ not closed properly )
    if (isset($_POST["subtitle"])) {

    Instead of using isset(), use just empty().

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom meta saving not working!’ is closed to new replies.
Skip to toolbar