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.

im having trouble using "add post meta" and "get post meta" : i have a new meta box in my taxonomy : i always have the value "empty meta" inside the input field, even after i enter anything : like 'banana' , i publish the article, and same thing : inside the field there is one more time "empty meta" ... (is it a problem with $post->id? i never really know if $post will be 'understood' depending how it is used)

/* Prints the box content */
    function myplugin_inner_custom_box() {
        // Use nonce for verification
        wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' );
        $met = get_post_meta($post->ID, 'champ', true)? get_post_meta($post->ID, 'champ', true) : 'empty meta';

        /* input */
        echo '<label for="myplugin_new_field">';
        _e("Description field : ", 'myplugin_textdomain' );
        echo '</label> ';
        echo '<input type="text" id="champ" name="champ" value="'.$met.'" />';

also, i tried to make the meta appear in my page, so i put :

<?php
if ( get_post_meta($post->ID, 'champ_key', true) ) {
$meta = get_post_meta($post->ID, "champ_key", true );
} else {
$meta = 'allo';
}
?>

but nothing appears on the page.

Do you know how i could go ahead with those post meta?

Thanks

EDIT

here's the complete code : if somebody could try it, i might doing something wrong, but i can't find out what. (the whole code is inside a widget page)

/*
 *
 * add meta box : habillage
 *
 */
add_action( 'add_meta_boxes', 'myplugin_add_custom_box' );
add_action( 'save_post', 'myplugin_save_postdata' );

/* Adds a box to the main column on the Post and Page edit screens */
function myplugin_add_custom_box() {
add_meta_box(
    'test1',
    __( 'My test1', 'myplugin_textdomain' ),
    'myplugin_inner_custom_box',
    'post'
);
add_meta_box(
    'test2',
    __( 'My test2', 'myplugin_textdomain' ),
    'myplugin_inner_custom_box',
    'movies'
);
}


/* Prints the box content */
function myplugin_inner_custom_box() {
global $post;
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' );
$met = get_post_meta($post->ID, 'champ', true)? get_post_meta($post->ID, 'champ', true) : 'empty meta';

    /* input */
echo '<label for="myplugin_new_field">';
_e("Description field : ", 'myplugin_textdomain' );
echo '</label> ';
echo '<input type="text" id="champ" name="champ" value="'.$met.'" />';




/* select list */
echo '<label for="my_list_field">';
_e("Description for this field", 'myplugin_textdomain' );
echo '</label> ';
echo '<select name="my_list_field" id="my_list_field">';

print_r ($post->ID);

global $post;

$s_query = new WP_Query( array(
'suppress_filters' => false,
'post_type' => 'movies'));
while($s_query->have_posts()):$s_query->the_post();

    $sname = $post->post_title;
    $s_output2 ='';
    $s_output2 .= '<option value="'.$post->ID.'" >';
    $s_output2 .= $post->post_title.' : allo';
    $s_output2 .= '</option>';
    echo $s_output2;

endwhile ;
echo '</select>';
wp_reset_query();
}


/* When the post is saved, saves our custom data */
function myplugin_save_postdata( $post_id ) {
  // verify if this is an auto save routine.
  // If it is our form has not been submitted, so we dont want to do anything
  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
      return;

  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times
  if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) )
  return;

  // Check permissions
  if ( 'page' == $_POST['post_type'] )
  {
    if ( !current_user_can( 'edit_page', $post_id ) )
    return;
  }
  else
  {
    if ( !current_user_can( 'edit_post', $post_id ) )
        return;
  }
  // OK, we're authenticated: we need to find and save the data
  //$mydata = $_POST['myplugin_new_field'];
  // Do something with $mydata
  // probably using add_post_meta(), update_post_meta()
  add_post_meta($post->ID, "champ_key", 'banana');

return $post_id;
//return $mydata;
}
?>
share|improve this question

3 Answers 3

here's the complete code : if somebody could try it, i might doing something wrong, but i can't find out what. (the whole code is inside a widget page)

/*
 *
 * add meta box : habillage
 *
 */
add_action( 'add_meta_boxes', 'myplugin_add_custom_box' );
add_action( 'save_post', 'myplugin_save_postdata' );

/* Adds a box to the main column on the Post and Page edit screens */
function myplugin_add_custom_box() {
add_meta_box(
    'test1',
    __( 'My test1', 'myplugin_textdomain' ),
    'myplugin_inner_custom_box',
    'post'
);
add_meta_box(
    'test2',
    __( 'My test2', 'myplugin_textdomain' ),
    'myplugin_inner_custom_box',
    'movies'
);
}


/* Prints the box content */
function myplugin_inner_custom_box() {
global $post;
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' );
$met = get_post_meta($post->ID, 'champ', true)? get_post_meta($post->ID, 'champ', true) : 'empty meta';

    /* input */
echo '<label for="myplugin_new_field">';
_e("Description field : ", 'myplugin_textdomain' );
echo '</label> ';
echo '<input type="text" id="champ" name="champ" value="'.$met.'" />';




/* select list */
echo '<label for="my_list_field">';
_e("Description for this field", 'myplugin_textdomain' );
echo '</label> ';
echo '<select name="my_list_field" id="my_list_field">';

print_r ($post->ID);

global $post;

$s_query = new WP_Query( array(
'suppress_filters' => false,
'post_type' => 'movies'));
while($s_query->have_posts()):$s_query->the_post();

    $sname = $post->post_title;
    $s_output2 ='';
    $s_output2 .= '<option value="'.$post->ID.'" >';
    $s_output2 .= $post->post_title.' : allo';
    $s_output2 .= '</option>';
    echo $s_output2;

endwhile ;
echo '</select>';
wp_reset_query();
}


/* When the post is saved, saves our custom data */
function myplugin_save_postdata( $post_id ) {
  // verify if this is an auto save routine.
  // If it is our form has not been submitted, so we dont want to do anything
  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
      return;

  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times
  if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) )
  return;

  // Check permissions
  if ( 'page' == $_POST['post_type'] )
  {
    if ( !current_user_can( 'edit_page', $post_id ) )
    return;
  }
  else
  {
    if ( !current_user_can( 'edit_post', $post_id ) )
        return;
  }
  // OK, we're authenticated: we need to find and save the data
  //$mydata = $_POST['myplugin_new_field'];
  // Do something with $mydata
  // probably using add_post_meta(), update_post_meta()
  add_post_meta($post->ID, "champ_key", 'banana');

return $post_id;
//return $mydata;
}
?>
share|improve this answer
    
try this: add_post_meta($post_id, "champ", $_POST['champ'], true); update_post_meta($post_id, "champ", $_POST['champ']); –  Michael May 28 '11 at 9:36
    
@Michael : THANK YOU SO MUCH, it works perfect, thanks for your help Michael! –  Paul_p May 28 '11 at 23:49
    
Note: clarifications of the original question should be added to the original question, as an edit, rather than posted as an "answer". –  Chip Bennett Oct 1 '11 at 12:42
    
@Michael please move your solution to an answer to this can be cleaned up –  Rarst Oct 1 '11 at 15:27
    
copied the code suggestion from my comment into the answer section; –  Michael Oct 1 '11 at 16:38

Alright, a couple things are an issue here.

First, in your meta box callback (myplugin_inner_custom_box), you don't need to globalize post. WordPress passes the post object as the first argument to that function. So you'd change it a bit:

<?php
function myplugin_inner_custom_box( $post ) {
// blah blah blah
}

The ternary statement to get your post meta is unnecessary. get_post_meta returns an empty string if the given meta key isn't yet set.

$met = get_post_meta($post->ID, 'champ', true);

Now, in your saving function, you should probably use update_post_meta instead of add_post_meta. As it's set now, add_post_meta will continue to add multiple meta values with the same key, which is probably not what you want.

Also, you're going to use $post_id, which you passed into your saving function in your new updated post meta called rather that $post->ID.

So...

<?php
function myplugin_save_postdata( $post_id ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return;

    if ( ! isset( $_POST['myplugin_noncename'] ) || ! wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) ) 
        return;

    if ( 'page' == $_POST['post_type'] )
    {
     if ( !current_user_can( 'edit_page' ) )
     return;
    }
    else
    {
     if ( !current_user_can( 'edit_post' ) )
         return;
    }

    if( isset( $_POST['champ'] ) )
        update_post_meta( $post_id, "champ", $_POST['champ'] );
}

In your code above, you're saving the meta key with the name 'champ_key' then trying to retrieve it with the key 'champ'. Can't do that! Save and grab the post meta with the same key.

Your code, revised: http://pastie.org/2438831 (I took out the add_meta_box to movie call, and the select box, as I don't have a movie post type).

share|improve this answer

try and add global $post; direct inside your function.

edit: answer moved from comment to here:

try this code:

    add_post_meta($post_id, "champ", $_POST['champ'], true); 
update_post_meta($post_id, "champ", $_POST['champ']);
share|improve this answer
    
hi, no unfortunately, it's the same. Could you try it with my code? i put the code in the next post, (as "answer") this is what i put in a plugin "my_widget", if you can just have a look ... –  Paul_p May 28 '11 at 0:47

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.