Ok, so I have registered a few custom post types and a few taxonomies. Now, for the life of me, I cannot figure out the code I need to add a Custom Field to my Custom Post Type.
I need a drop down and a single line text area. But I also need to have separate fields for post types. So, say post type one has 3 fields and post type 2 has 4 fields but the fields are different.
Any tips would help I have looked at the codex and found something but cannot make sense of what I need to add to my functions.php
file
This is probably more complicated than you think, I would look into using a framework:
If you want to write your own , here are some decent tutorials:
Add/edit the
supports
argument ( while usingregister_post_type
) to include thecustom-fields
to post edit screen of you custom post type:Source: https://codex.wordpress.org/Using_Custom_Fields#Displaying_Custom_Fields
Although you should have to add some validation, this action does not seem to be complicated for the current version of WordPress.
Basically you need two steps to add a Custom Field to a Custom Post Type:
These steps are globally described here: http://wordpress.org/support/topic/is-it-possible-to-add-an-extra-field-to-a-custom-post-type
Example:
Add a Custom Field called “function” to a Custom Post Type called “prefix-teammembers”.
First add the metabox:
If your add or edit a “prefix-teammembers” the
add_meta_boxes_{custom_post_type}
hook is triggered. See http://codex.wordpress.org/Function_Reference/add_meta_box for theadd_meta_box()
function. In the above call ofadd_meta_box()
isprefix_teammembers_metaboxes_html
, a callback to add your form field:In the second step you have your custom field to the database. On saving the
save_post_{custom_post_type}
hook is triggered (since v 3.7, see: https://stackoverflow.com/questions/5151409/wordpress-save-post-action-for-custom-posts). You can hook this to save your custom field:There are various plugins for custom meta boxes and custom fields. If you look at a plugin that focuses on developers, then you should try Meta Box. It’s lightweight and very powerful.
If you’re looking for a tutorial on how to write code for a meta box / custom fields, then this is a good start. It’s the first part of a series that might help you refine the code to make it easy to extend.
I know this question is old but for more info about the topic
WordPress has built-in support for custom fields. If you have a custom post type then all you need is to include ‘custom-fields’ inside the support array inside of register_post_type as answered by @kubante
Note that this option is also available for native post types like posts and pages you just need to turn it on
Now This custom field is very basic and accepts a string as a value. In many cases that’s fine but for more complex fields, I advise that you use the ‘Advanced Custom Fields’ plugin
The link to thinkvitamin.com in the accepted answer is no longer live. That website has switched it’s domain to treehouse.com. I am only answering here because I don’t have enough reputation to comment. The content of that expired link is available here: https://blog.teamtreehouse.com/create-your-first-wordpress-custom-post-type for anyone who needs it. Goodluck!
If you think that you have done everything correctly and you are using WordPress 5.7 probably all you need is to show it in the additional panel.
click 3 vertical dot on the top left > preferences > Panels
of course this is after you make sure the post type has correct configuration.
Perfect knowledge