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.

Well, I created my custom post, but I am wondering why is this if not for a customizable post page? so, I am guessing in some way, I could add new fields on this custom post and publish it as if it were a normal post WITHOUT installing Advance Custom Fields plugin and so on, just one simple done by myself.

DO anybody know any about this??

Thanks.

share|improve this question
    
Welcome at WPSE. I am sorry but I have some difficulties understanding your question. Could you maybe find someone to help you with your english? What I think I have understood is that you want to add custom fields to a custom post type? –  kraftner Jun 6 at 22:43
    
yes, is exactly that, I was wondering why wordpress let you create a custom type of post... if not to create it with custom fields as well... Could you help me with that? –  jpganz18 Jun 6 at 22:48
    
You can add custom field to custom post types (CPT). See the Codex page on Custom Fields, to start with. –  Pat J Jun 7 at 1:26

1 Answer 1

up vote 1 down vote accepted

I think you're looking for add_meta_box

http://codex.wordpress.org/Function_Reference/add_meta_box

// Metabox actions
add_action( 'add_meta_boxes', 'metabox_create' ); // create
add_action( 'save_post', 'metabox_save' ); // save

// Create metabox
function metabox_create() {
  add_meta_box( 'metabox_name', 'Metabox Title', 'metabox_content', 'my_custom_post_type', 'normal', 'high' );
}

// metabox content
function metabox_content() {
  global $post;

  // custom field data
  $custom = get_post_custom( $post->ID );
  $field_name = $custom['field_name'][0];

  // contents
  ?>
  <label for="field_name">Field name</label>
  <input name="field_name" id="field_name" value="<?php echo esc_attr( $field_name ); ?>" />
  <?php
}

// save metabox fields
function metabox_save() {
  global $post;

  // Ignore autosave
  if ( defined( 'DOING_AUTOSAVE' ) && true === DOING_AUTOSAVE ) { return; }

  // Save post meta.
  update_post_meta( $post->ID, 'field_name', $_POST['field_name'] );
}
share|improve this answer
    
Thanks, I think is similar, how can I test it? where should I put this code? I got an undefined function error when I just copy on my main plugin file –  jpganz18 Jun 6 at 23:10
    
I revised the code. Originally written from memory, but forgot the add_action calls. What function is it saying is undefined? You also might want to either move these calls into a class or prefix the function names with the name of your post type to avoid conflicts. –  Joey Yax Jun 6 at 23:24

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.