WordPress.org

Forums

Custom meta saving not working! (5 posts)

  1. Steekvlam
    Member
    Posted 1 year ago #

    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?

  2. ianhaycox
    Member
    Posted 1 year ago #

    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.

  3. Steekvlam
    Member
    Posted 1 year ago #

    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?

  4. Ravinder Kumar
    Member
    Posted 1 year ago #

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

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

Topic Closed

This topic has been closed to new replies.

About this Topic