i created a post meta, to add the name of a specific "author" to my articles. But if i enter the meta of the first article, all the other articles copy the same meta. If i enter then a second name in the meta of the second article, all the other articles copy this second "meta", as if i entered this second name for each article.

How can i specify the meta for only one article? in the admin panel : if i did not specify the author, i still have "empty meta", which is the text i specify in this case in functions.php . BUT, in the page on internet, it shows the name of the last "meta" name written in any of the article. I hope my description is clear enough, please tell me for more informations.

Here's the main lines of the code :

$met = get_post_meta($post->ID, 'champ', true)? get_post_meta($post->ID, 'champ', true) : 'empty meta';

echo '<input type="text" id="champ" name="champ" value="'.$met.'" />';
add_post_meta($post_id, "champ", $_POST['champ'], true);
update_post_meta($post_id, "champ", $_POST['champ']);
  • "i created a post meta", do you mean that you created a custom meta box? If yes, then you need to make sure that you properly save the custom post meta. a good starting article on wptheming.com/2010/08/custom-metabox-for-post-type – user2015 May 30 '11 at 2:06
  • @Piet : thanks, yes i meant a new meta box, i basically did almost the same as the tutorial you gave me, and i tried also with their code, but it's again the same result : only difference, i'm using it on the "articles"... Could you try it on the articles? i got the good value, but if i don't fill in the other inputs, they take the last "post meta" submitted, and put it for every other articles... strange... – Paul_p May 30 '11 at 4:28
  • if you follow the tutorial to the letter, then you will see that it works. My guess is that the difference lies in the word "almost" in your sentence "i basically did almost the same as the tutorial"... – user2015 May 30 '11 at 6:40
  • can you post all of the exact code you are using – Bainternet May 30 '11 at 12:52
  • @Bainternet : hi, i put the code in the next answer, with a description of the problems i have – Paul_p May 30 '11 at 15:44

You don't need to check if meta data exists prior to adding/updating it. update_post_meta does this for you. (See Codex)

update_post_meta($post_id, $meta_key, $meta_value, $prev_value);

The fourth (optional) $prev_value argument, if set, will update only $meta_key-$prev_value pairs. If not set, it will update all values for that $meta_key for that post. If it can't find that $meta_key, it will create a new record.

The add_post_meta, by default, will add a record regardless if the key already exists. It seems this is what is happening, so try var_dump-ing get_post_meta($post->ID, $key, FALSE) and die-ing to check what it is returning.


I've not checked this, but since your post meta does not have an underscore, it may appear twice on the edit post page (once in your custom metabox, and once in the custom fields section (this may be hidden, check the screen options). This could be causing both the new meta value the old values to be saved to the database...


Here is the code of my plugin, to add a post meta. The problems are :

  • after some tests, i figured out that only the "older" posts "copy" the post meta, but not the newest ones. So let's say it is not the main problem i have. (if you have an explanation for this, i'll take it though ;) )

  • the main problem is now : if i update a meta, it doesn't update it but create a new meta, for the same post_id, same meta_key, and thus same meta_value.

I'm using here the function "save" from the tutorial (above).

        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() {
                __( 'Author : ', 'myplugin_textdomain' ),
                __( 'My test2', 'myplugin_textdomain' ),

        /* Prints the box content */
        function myplugin_inner_custom_box() {
            global $post;
            echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
            wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

            $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("Author name : ", 'myplugin_textdomain' );
            echo '</label> ';
            echo '<input type="text" id="champ" name="champ" value="'.$met.'" />';
            /* select list */
            echo '<label for="my_list_field">';
            _e("Already in database : ", 'myplugin_textdomain' );
            echo '</label> ';
            echo '<select name="my_list_field" id="my_list_field">';
            global $post;

            $s_query = new WP_Query( array(
            'suppress_filters' => false,
            'post_type' => 'movies'));

                $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>';

        // Save the Metabox Data
        function wpt_save_events_meta($post_id, $post) {

            if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
            return $post->ID;
            if ( !current_user_can( 'edit_post', $post->ID ))
                return $post->ID;
            $events_meta['champ'] = $_POST['champ'];
            foreach ($events_meta as $key => $value) { // Cycle through the $events_meta array!
                $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
                if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
                    update_post_meta($post->ID, $key, $value);
                } else { // If the custom field doesn't have a value
                    add_post_meta($post->ID, $key, $value);
                if(!$value) delete_post_meta($post->ID, $key); // Delete if blank

        add_action('save_post', 'wpt_save_events_meta', 1, 2); // save the custom fields
  • Please update your question rather than posting further question as an answer. If your question has changed sufficiently, accept the correct answer for this question and ask a new one :) – Stephen Harris Jun 20 '12 at 9:11

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.