0

I'm trying to set a meta key value of either "yes" or null with the checkbox so I can select posts using a query that checks if the meta key exists and not use the value of the meta_key. I don't want to add a meta_key to all existing posts, and want the meta_key to be deleted when unchecked.

But update_post_meta at the end of this function doesn't delete the meta_key featured-checkbox on update. How can I get the function to delete the key?

// Featured Post metabox

function prfx_featured_meta() {
    add_meta_box( 'prfx_meta', __( 'Featured Posts', 'prfx-textdomain' ), 'prfx_meta_callback', 'post', 'side', 'high' );
}
add_action( 'add_meta_boxes', 'prfx_featured_meta' );

/**
 * Outputs the content of the meta box
 */

function prfx_meta_callback( $post ) {
    wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
    $prfx_stored_meta = get_post_meta( $post->ID );
    ?>

 <p>
    <span class="prfx-row-title"><?php _e( 'Check if this is a featured post: ', 'prfx-textdomain' )?></span>
    <div class="prfx-row-content">
        <label for="featured-checkbox">
            <input type="checkbox" name="featured-checkbox" id="featured-checkbox" value="yes" <?php if ( isset ( $prfx_stored_meta['featured-checkbox'] ) ) checked( $prfx_stored_meta['featured-checkbox'][0], 'yes' ); ?> />
            <?php _e( 'Featured Item', 'prfx-textdomain' )?>
        </label>

    </div>
</p>   

    <?php
}

/**
 * Saves the custom meta input
 */
function prfx_meta_save( $post_id ) {

    // Checks save status
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

    // Exits script depending on save status
    if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
        return;
    }

    // Checks for input and sanitizes/saves if needed
    // How do I delete the featured-checkbox key and not update it?
    if( isset( $_POST[ 'featured-checkbox' ] ) ) {
        update_post_meta( $post_id, 'featured-checkbox', sanitize_text_field( $_POST[ 'featured-checkbox' ] ) );
    }

}
add_action( 'save_post', 'prfx_meta_save' );

Update 12/22/16: this change works:

// Checks for input and saves - save checked as yes or deletes meta_key if no
if( isset( $_POST[ 'featured-checkbox' ] ) ) {
    update_post_meta( $post_id, 'featured-checkbox', 'yes' );
} else {
        delete_post_meta( $post_id, 'featured-checkbox' );
}

1 Answer 1

2

You're looking for delete_post_meta($post_id, $meta_key, $meta_value);

Parameters

$post_id (integer) (required) The ID of the post from which you will delete a field. Default: None

$meta_key (string) (required) The key of the field you will delete. Default: None

$meta_value (mixed) (optional) The value of the field you will delete. This is used to differentiate between several fields with the same key. If left blank, all fields with the given key will be deleted. Default: Empty

1
  • Thanks, you're right; and I added a condiitonal to check if yes, save, if no, delete. Commented Dec 22, 2016 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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