1

I was trying to add a Custom Post type and a Custom Meta box. Which I've done. But I'm facing another problem which is. When I give any value into the Custom Metabox I created its not saving the data on backend. Its perfectly working on Frontend.

Now since its not keeping the value in backend whenever I update the post I have to enter the value each time.

Here is what I've done so far:

function news_post_type() {
  $labels = array(
    'name'               => _x( 'News', 'post type general name' ),
    'singular_name'      => _x( 'News', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New' ),
    'edit_item'          => __( 'Edit News' ),
    'new_item'           => __( 'New News' ),
    'all_items'          => __( 'All News' ),
    'view_item'          => __( 'View News' ),
    'search_items'       => __( 'Search News' ),
    'not_found'          => __( 'Not found' ),
    'not_found_in_trash' => __( 'Not found in the Trash' ), 
    'parent_item_colon'  => '',
    'menu_name'          => 'News'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'This section contains the News section of the site',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments'),
    'has_archive'   => true,
  );
  register_post_type( 'news', $args ); 
}
add_action( 'init', 'news_post_type' );

and for the metabox :

add_action( 'add_meta_boxes', 'news_date_box' );

function news_date_box() {
    add_meta_box( 
        'news_date_box',
        __( 'News Date', 'myplugin_textdomain' ),
        'news_date',
        'news',
        'side',
        'high'
    );
}

function news_date( $post ) {

  wp_nonce_field( plugin_basename( __FILE__ ), 'news_date_content_nonce' );

  echo '<input type="text" id="news_date" name="news_date" placeholder="Enter Date"/>';

}

add_action( 'save_post', 'news_date_save' );

function news_date_save( $post_id ) {

  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
  return;

  if ( !wp_verify_nonce( $_POST['news_date_content_nonce'], plugin_basename( __FILE__ ) ) )
  return;

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

  $news_date = $_POST['news_date'];
  update_post_meta( $post_id, 'news_date', $news_date );
}

I searched for the solution but didn't find any and ended up here. I may made a mistake in above code or I have to add additional codes. Any suggestion how to solve this situation.

Thanks in advance.

1

Modify your news_date() function to be as follows:

function news_date( $post ) {

wp_nonce_field( plugin_basename( __FILE__ ), 'news_date_content_nonce' );

// get the current value of 'news_date' to use in the <input>
$value = get_post_meta ($post->ID, 'news_date', true) ;

echo '<input type="text" id="news_date" name="news_date" placeholder="Enter Date" value="' . $value . '"/>';

}

Another suggestion would be to change

add_action( 'save_post', 'news_date_save' );

to

add_action( 'save_post_news', 'news_date_save' );

and then you're assured that your news_date_save() function is only called when a post whose post_type == 'news' is saved.

Doing so will allow you to get rid of the if ( 'page' == $_POST['post_type'] ) { if/then/else, because then the condition in that if will never evaluate to true.

  • Hi, thank you so much. Modifying the news_date() function worked. Thanks again :) – Muhammad Russell Feb 26 '17 at 21:01

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.