RTB
RTB

Reputation: 5823

How to add meta information to a custom post type in wp-admin?

I've set up 3 custom post types for the wordpress website I'm working on using the following code in functions.php:

if ( function_exists('add_action') )
{
    add_action( 'init', 'create_post_types' );
    function create_post_types()
    {
        register_post_type('dj',Array('labels' => Array('name' => "DJ’s",'singular_name' => "DJ"),'public' => true,'has_archive' => true));
        register_post_type('gerecht',Array('labels' => Array('name' => "Gerechten",'singular_name' => "Gerecht"),'public' => true,'has_archive' => true));
        register_post_type('agenda',Array('labels' => Array('name' => "Agenda",'singular_name' => "Evenement"),'public' => true,'has_archive' => true));
    }
}

But in the wp-admin screen i can't add any meta info to the post. How do i fix this, or is it just not possible?

Edit: I don't want to use any plugin for this, but write the code myself.

Upvotes: 1

Views: 862

Answers (2)

jayarjo
jayarjo

Reputation: 16726

You need to add supports field to your initialization array, like this:

register_post_type('dj', Array(
    'labels' => Array(
         'name' => "DJ’s",
         'singular_name' => "DJ"
     ),
    'public' => true,
    'has_archive' => true,
    'supports' => array('title', 'editor', 'custom-fields') // notice 'custom-fields'
));

By default it's only title and editor, that's why you probably do not get them in the backend.

Full list of supported features looks like this:

  • title: Text input field to create a post title.
  • editor: Content input box for writing.
  • comments: Ability to turn comments on/off.
  • trackbacks: Ability to turn trackbacks and pingbacks on/off.
  • revisions: Allows revisions to be made of your post.
  • author: Displays a select box for changing the post author.
  • excerpt: A textarea for writing a custom excerpt.
  • thumbnail: The thumbnail (featured image in 3.0) uploading box.
  • custom-fields: Custom fields input area.
  • page-attributes: The attributes box shown for pages. This is important for hierarchical post types, so you can select the parent post.

Here is a nice writeup on the topic: Custom post types in WordPress.

Also here is the better place to ask WordPress related questions: WordPress Answers.

Upvotes: 1

SMacFadyen
SMacFadyen

Reputation: 3165

Personally, I would use the plugin called Advanced Custom Fields, which offers quite a nice interface to do this, as it offers a wide range of options.

You can use the above in conjunction with Custom Post Type UI, which allows you to create custom post types and taxonimies with a UI. FYI, you can 'Get the code' and place it into your functions.php.

An example of this:

register_post_type('custom-post-name', array(  'label' => 'Custom Post Label','description' => '','public' => true,'show_ui' => true,'show_in_menu' => true,'capability_type' => 'post','hierarchical' => false,'rewrite' => array('slug' => ''),'query_var' => true,'exclude_from_search' => false,'supports' => array('title','editor','custom-fields',),'labels' => array (
  'name' => 'Custom Post Name',
  'singular_name' => 'Value',
  'menu_name' => 'Custom Post Menu Name',
  'add_new' => 'Add Item',
  'add_new_item' => 'Add New Item',
  'edit' => 'Edit',
  'edit_item' => 'Edit Item',
  'new_item' => 'New Item',
  'view' => 'View Item',
  'view_item' => 'View Item',
  'search_items' => 'Search Custom Post',
  'not_found' => 'No Item(s) Found',
  'not_found_in_trash' => 'No Item(s) Found in Trash',
  'parent' => 'Parent Value',
),) );

you might want to look at the array and add your own descriptive data, i.e. where it says Item for singular_name and name.

Upvotes: 1

Related Questions