1

I'm trying to create a metabox in a custom post type. When I attempt to insert information into the Listing Price box and update the post, the information is not saved and disappears once the page reloads. Please take a look at my code below and let me know if you see anything that could be causing this issue. Thanks!

EDIT: I figured it out. I'm still not sure why my previous method wouldn't work. I changed the input field value to ID, 'listing_price', true ); ?> and now the box updates!

<?php

function cns_add_custom_metabox(){
    add_meta_box(
        'cns_meta',
        __('ForesTree Listing'),
        'cns_meta_callback',
        'listing',
        'normal',
        'high'
    );
}
add_action( 'add_meta_boxes', 'cns_add_custom_metabox'  );

function cns_meta_callback( $post ){
    wp_nonce_field( basename(__FILE__), 'cns_listing_nonce' );
    $cns_stored_meta = get_post_meta( $post->ID );
    ?>

    <div>
        <div class="meta-row">
            <div class="meta-th">
                <label for="listing-price" class="cns-row-title">Listing Price</label> 
            </div>
            <div class="meta-td">
                <input type="number" name="listing_price" id="listing-price" value="<?php if ( ! empty ( $cns_stored_meta['listing_price'] ) ) { echo esc_attr( $cns_store_meta['listing_price'][0] ); } ?>"/>
            </div>
        </div>
        <div class="meta-row">
            <div class="meta-th">
                <span>Listing Description</span>
            </div>
        </div>
        <div class="meta-editor"></div>
        <?php
            $content = get_post_meta( $post->ID, 'listing_description', true );
            $editor_id = 'listing_description';
            $settings = array(
                'textarea_rows' => 8,
                'media_buttons' => false
            );

            wp_editor( $content, $editor_id, $settings );

        ?>
    </div>

    <?php
}

function cns_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[ 'cns_listing_nonce' ] ) && wp_verify_nonce( $_POST[ 'cns_listing_nonce' ], basename(__FILE__) ) ) ? 'true' : 'false';

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

    if( isset( $_POST['listing_price'] ) ) {
        update_post_meta( $post_id, 'listing_price', sanitize_text_field( $_POST[ 'listing_price' ] ) );
    }
}
add_action( 'save_post', 'cns_meta_save' );
  • Hi, please either add your solution as an answer or select an answer that's closest to your solution. – Joshua Jan 7 '16 at 23:38
1
<input type="number" name="listing_price" id="listing-price" value="<?php if ( ! empty ( $cns_stored_meta['listing_price'] ) ) { echo esc_attr( $cns_store_meta['listing_price'][0] ); } ?>"/>

You forget to put "d" where you echo your listing price.

You also not write code for listing_description for save listing editor content.

if( isset( $_POST['listing_description'] ) ) {
        update_post_meta( $post_id, 'listing_description', sanitize_text_field( $_POST[ 'listing_description' ] ) );
    }

I hope this will help for you.

1

Hi you are missing your meta name when retrieving the meta value. You just given the post ID, you need to input the meta name too.

 $cns_stored_meta = get_post_meta( $post->ID );

Should be

 $cns_stored_meta = get_post_meta( $post->ID , 'listing_price');

Suggestion If you don't want to get the meta in array and then parse it with index like $cns_stored_meta[0] then you can make the third parameter as true which will return the value as string not an array

$cns_stored_meta = get_post_meta( $post->ID , 'listing_price', true);

now you can directly access $cns_stored_meta

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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