Before adding a metabox to a custom post type we need to create the custom post type.
Check this post where you can find a full class to create your custom post type.

So following on this example our custom post type is cpt_examples.

In the __construct function of our custom post type class add the following:

1
2
add_action( 'add_meta_boxes', array( $this, 'add_meta_box') );
add_action( 'save_post',      array( $this, 'save_meta_box_data' ) );

And then create the functions in the class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
 * add_meta_box
 */
public function add_meta_box(){
 
	add_meta_box( 'cpt_examples_meta', 'CPT Examples Extra Info', array( $this, 'display_meta_form' ), 'cpt_examples', 'advanced', 'high' );
}
 
/**
 * display_meta_form	
 */
 
public function display_meta_form( $post ) {
 
	wp_nonce_field( 'cptexamples_meta_box', 'cptexamples_meta_box_nonce' );
 
	$my_first_field  = get_post_meta( $post->ID, 'cpt_first_meta_field', true );
	$my_second_field = get_post_meta( $post->ID, 'cpt_second_meta_field', true );
 
		echo '<div class="wrap">';
		echo '<label for="cpt_first_meta_field">' . _e( 'First Meta Field', 'cpt_domain' ) . '</label> <br/>';
		echo '<input class="text" type="text" id="cpt_first_meta_field" name="cpt_first_meta_field" value="' . esc_attr( $my_first_field ) . '"   />';
		echo '</div>';
 
		echo '<div class="wrap">';
		echo '<label for="cpt_second_meta_field">' . _e( 'Second Meta Field', 'cpt_domain' ) . '</label>  <br/>';
		echo '<input class="text" type="text" id="cpt_second_meta_field" name="cpt_second_meta_field" value="' . esc_attr( $my_second_field ) . '"   />';
		echo '</div>';
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
 * save_meta_box_data
 * function called on save_post hook to sanitize and save the data
 */
 
public function save_meta_box_data( $post_id ){
 
  // Check if nonce is set.
    if ( ! isset( $_POST['cptexamples_meta_box_nonce'] ) ) {
	  return;
    }
 
  // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST['cptexamples_meta_box_nonce'], 'cptexamples_meta_box' ) ) {
	   return;
    }
 
  // If autosave, don't do anything
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
	  return;
    }
 
  // Check the user's permissions.
    if ( isset( $_POST['post_type'] ) && $_POST['post_type'] == 'cpt_examples' ) {
            if ( ! current_user_can( 'edit_page', $post_id ) ) {
		     return;
	    }
 
    } else {
            if ( ! current_user_can( 'edit_post', $post_id ) ) {
		     return;
	    }
    }
 
   // Save the information into the database
       if ( isset( $_POST['cpt_first_meta_field'] ) ) {
 
             $my_first_meta_field = sanitize_text_field( $_POST['cpt_first_meta_field'] );
	     update_post_meta( $post_id, 'cpt_first_meta_field', $my_first_meta_field );
	}
 
       if ( isset( $_POST['cpt_second_meta_field'] ) ) {
 
             $my_second_meta_field = sanitize_text_field( $_POST['cpt_second_meta_field'] );
	     update_post_meta( $post_id, 'cpt_second_meta_field', $my_second_meta_field );
	}
 
 
}

Also view this snippet to add the meta box just after the title.

Add meta box to a custom post type

4 thoughts on “Add meta box to a custom post type

  • August 1, 2016 at 00:38
    Permalink

    Thank you for this, Lucy! Quick question–in order to display the input from a metabox (on a custom post type) on the main posts page, do you have to do anything else? I implemented your snippets, and they seem to be working, but the data from the metaboxes do not appear on my maid feed page.

    Reply
    • August 1, 2016 at 11:00
      Permalink

      Hi Lisa,
      If the data of your metabox is being saved correctly in the dabase to the postmeta table, you should be able to access it through the function get_post_meta() and just output the result of it.
      https://developer.wordpress.org/reference/functions/get_post_meta/

      Example:
      $my_metabox_data = get_post_meta( get_the_ID, ‘my_metabox_data’, true );
      echo esc_html( $my_metabox_data );

      Hope that helps, let me know if you have any questions.

  • August 2, 2016 at 18:53
    Permalink

    That did help! Thanks so much! This post was super helpful to me!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *