Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

troubles with get_post_meta (and saving it)

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;
}
?>

Related Posts:

  1. Execute action after post is saved with all related post_meta records (data)
  2. Add post meta based on another post meta value before publish post
  3. Get updated post meta on save_post action?
  4. Correct processing of `$_POST`, following WordPress Coding Standards
  5. Delete post meta conditionally after save post
  6. Set default Custom Post Meta Value
  7. Save post meta foreach loop
  8. wp_insert_post inside save_post adds wrong metadata to inserted post
  9. post_title in save_post action
  10. Auto save title as custom meta field value
  11. Save frontend submission form field as custom field
  12. get_post_meta() returns nothing in save_post, publish_post, wp_after_insert_post
  13. if condition from post_meta not working in save_post
  14. Get posts by meta value
  15. advanced custom fields update_field for field type: Taxonomy
  16. update_post_meta not saving when value is zero
  17. Restrict post edit/delete based on user ID and custom field
  18. get_post_meta returning empty string when data shows in the database
  19. publish_post action hook doesn’t give post_meta_data
  20. Remove post meta keys
  21. delete unused postmeta
  22. Query between two meta values?
  23. Using Advanced Custom Field (ACF) to insert meta description on each page
  24. How to add meta tag to wordpress posts filter?
  25. Metabox Data not being saved [closed]
  26. Options to get my custom post type metadata via the WordPress API
  27. Generate an Email address from that of the Post Author
  28. How to get the total number of meta_values based on a custom post type?
  29. Does “update_post_meta” check if value is the same before updating?
  30. Add a meta value if admin , editor or any other user have open a post in edit mode
  31. How to show Published date and/or Modified date
  32. Finding the page id
  33. WP_POSTMETA – What do these values mean inside the data structure?
  34. Update Line Item Meta Data – WooCommerce API
  35. How to use update_post_meta inside wp_trash_post
  36. Unset field from an array not working as expected
  37. Update post_meta with WooCommere variation data
  38. What type of index should I use for postmeta?
  39. Meta function issue
  40. Custom RSS Feeds & Post Meta Data
  41. need some assistance with the checkboxes (selecting/deselecting featured posts)
  42. get_post_meta() not work for the first post inside the loop
  43. get_post_meta is showing file url in url bar
  44. Adding new value to existing meta key
  45. how to get a list of meta data fields assocaited with a custom post type
  46. Gutenberg: How to display meta field data in the block frontend (save function)
  47. How to check if a post meta key/value pair already exists for a specific post
  48. String taken from database table post_meta is showing HTML tags on Frontend
  49. displaying an error before update_post_meta
  50. Delete posts based on post meta data
  51. Save meta data with post, Without using any plugin [closed]
  52. Is it safe to add a new field to meta_value field?
  53. How do I edit the posted “by” and “on” in the post meta
  54. How to delete duplicate records in wp_postmeta database table?
  55. update_post_meta() adding bits in database [closed]
  56. Get posts ordered by meta data
  57. Editor meta box Showing but not saving
  58. Displaying values from related field – if empty shows current post type’s link and title
  59. get_post_meta inside echo [closed]
  60. WordPress postmeta: Store data as an array or single item?
  61. Display Post Meta in Header
  62. get_template_part() isn’t loading author information
  63. Front end update_post_meta Help
  64. Array of user ids to list of user names
  65. Conditional For get_the_author_meta
  66. Custom fields or something else
  67. get Insert id for meta field
  68. Is there a way combine posts meta_name?
  69. Format meta_value [closed]
  70. How can I query for posts using a date stored in post-meta?
  71. How to check post meta isset or not empty during publish/save post?
  72. Count Post and Page Views based on meta_value Using Shortcode in Dashboard Widget
  73. Removing Malware
  74. After updating the custom post type, metafields disappear from the post.php edit menu, how do I fix it?
  75. Add post meta data date to event
  76. URL from get_post_meta() is broken my URL
  77. Something adding an excessive meta description
  78. get_post_meta and add_post_meta not working
  79. Export media library with metadata and import into new blog
  80. how to query posts using value in meta post array
  81. Multiple meta key and value search in the query
  82. Re-pointing images to cloud storage
  83. undesrtanding get_post_meta function
  84. add_post_meta only adding 1 character
  85. Retrieve posts from meta key
  86. Assign postmeta in bulk
  87. Problem with ‘save_post’ hook not running
  88. Re-order posts inside tax query
  89. How do I delete element from a serialized array upon deletion of a post?
  90. Using mysql queries to delete custom post types based on meta_value
  91. Write query according to post_meta
  92. Why values dont shows in custom post column?
  93. get_post_meta of multiple posts?
  94. Counting number of identical meta keys
  95. I am stuck between post meta function to call unique id
  96. Chance post id into post name
  97. Post meta box data not saving
  98. Cannot read properties of undefined (reading ‘useEntityProp’)
  99. Search posts by meta key in admin and front
  100. sanitize meta input
Categories post-meta Tags post-meta, save-post
How can I convert everything from category X to have post format Link
How to remove date from showing in meta description

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress