Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I created a custom meta box with a textbox where the user can enter a custom value in as an additional identification tag. I thought I had it right but when I try to retrieve the value it is giving me the post ID instead of the custom value that was input.

I am sorry if the post gets too long but I want to be thorough. To get the post meta I am using this:

     <?php $meta_values = get_post_meta( the_ID(), newtheme_section_id); ?>

I am adding the code I used to create the meta box, and I am hoping someone can give me a helping hand. Thanks.

/**
 * Sets up custom section ID meta box in admin post and edit pages.
 *
 * @since newtheme 1.0
 */
add_action( 'load-post.php', 'section_id_meta_box_setup' );
add_action( 'load-post-new.php', 'section_id_meta_box_setup' );

/* Meta box setup function. */
function section_id_meta_box_setup() {

    /* Add meta box on the 'add_meta_boxes' hook. */
    add_action( 'add_meta_boxes', 'newtheme_section_id_meta_boxes' );

/* Save post meta on the 'save_post' hook. */
add_action( 'save_post', 'newtheme_save_section_id_meta', 10, 2 );
}

/* Create meta box to be displayed on the post editor screen. */
function newtheme_section_id_meta_boxes() {

    add_meta_box(
        'newtheme-section-id',          // Unique ID
        esc_html__( 'Custom Section ID', 'ID' ),        // Title
        'newtheme_section_id_meta_box',     // Callback function
        'post',                 // Admin page (or post type)
        'side',                 // Context
        'default'                   // Priority
    );
}

/* Display the post meta box. */
function newtheme_section_id_meta_box( $object, $box ) { ?>

    <?php wp_nonce_field( basename( __FILE__ ), 'newtheme_section_id_nonce' ); ?>

    <p>
        <label for="newtheme-section-id"><?php _e( "You can give your section a name and it will appear in the menus. ", 'Optional' ); ?></label>
        <br />
        <input class="widefat" type="text" name="newtheme-section-id" id="newtheme-section-id" value="<?php echo $text; ?>" size="30" />
    </p>
<?php }

/* Save the meta box's post metadata. */
function newtheme_save_section_id_meta( $post_id, $post ) {

    /* Verify the nonce before proceeding. */
    if ( !isset( $_POST['newtheme_section_id_nonce'] ) || !wp_verify_nonce( $_POST['newtheme_section_id_nonce'], basename( __FILE__ ) ) )
        return $post_id;

    /* Get the post type object. */
    $post_type = get_post_type_object( $post->post_type );

    /* Check if the current user has permission to edit the post. */
    if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
        return $post_id;

    /* Get the posted data and sanitize it for use as an HTML class. */
    $new_meta_value = ( isset( $_POST['newtheme-section-id'] ) ? sanitize_html_class( $_POST['newtheme-section-id'] ) : '' );

    /* Get the meta key. */
    $meta_key = 'newtheme_section_id';

    /* Get the meta value of the custom field key. */
    $meta_value = get_post_meta( $post_id, $meta_key, true );

    /* If a new meta value was added and there was no previous value, add it. */
    if ( $new_meta_value && '' == $meta_value )
        add_post_meta( $post_id, $meta_key, $new_meta_value, true );

    /* If the new meta value does not match the old value, update it. */
    elseif ( $new_meta_value && $new_meta_value != $meta_value )
        update_post_meta( $post_id, $meta_key, $new_meta_value );

    /* If there is no new meta value but an old value exists, delete it. */
    elseif ( '' == $new_meta_value && $meta_value )
        delete_post_meta( $post_id, $meta_key, $meta_value );

EDIT: I am trying to display the value that the users input into a new ID tag on the page that the posts appear so each post will display the custom id tag that the user entered.

Here is the code I am trying to do so with.

    <?php $post_id = get_the_ID(); ?>
    <?php $value = get_post_meta($post_id, 'newtheme_section_id'); ?>
    <id="<?php $value; ?>">

It seems when I do this I am getting a blank value. As in it is not returning anything. I know forsure that it is saving the data from the post page because the custom tag appears in the input box when you go to edit the post. What exactly am I doing wrong here?

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

Please take some time to read the Codex. the_ID echos content. That means that the ID is never being passed to get_post_meta. It is just echoed in place. From the same Codex Page:

Note: This function displays the ID of the post, to return the ID use get_the_ID().

So, use get_the_ID instead.

Second problem... quote your string. As you have it written PHP will try, and likely fail, to find a constant named newtheme_section_id. I don't think that is intended.

$meta_values = get_post_meta( get_the_ID(), 'newtheme_section_id' );

If that code is in the Loop, it should work.

share|improve this answer
    
Thank you soo much. I knew I wasn't doing something right. –  user1632018 Apr 20 '13 at 20:46
    
I just have one problem. I was able to correct my code so the custom name that the user inputs on the post page shows up when you go to edit the post, so I know it is saving properly. Although I am trying to display the custom value entered on each post as a ID tag. Although when I am doing this it is not returning any value at all. If I use the_ID() it still just returns the post id. Can you lend me a hand? I should add that I edited my post to better explain this. –  user1632018 Apr 20 '13 at 23:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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