A custom fields are, basically, the properties for a post type.
The creation phase of a custom field is divided in two parts:
Metabox is the graphic container of our custom fields. It will appear as a fieldsets in the admin page, containing our custom fields.
add_action('add_meta_boxes_{post_type_name}','add_metabox_function');
function add_metabox_function($post)
{
add_meta_box('id','title','metabox_field_function','post_type_name','normal','default');
}
This function will contain all necessary html, CSS and logical code to manage and show our custom fields.
function metabox_field_function()
{
//html code to show fields and php code to manage fields
}
One interesting thing is that you don’t have to define an html form and a submit button, because your code will be automatically wrapped in.
So: no form and no submit butto, but the normal html input tag.
After defining the html field in the previous step it’s time to manage the submit of your metadata.
The hook to define it is:
add_action('save_post','save_posttypename_meta', 10, 2);
function save_posttypename_meta($post_id, $post)
{
//php code to get fields from POST and save them into db
}
With this variable you can access directly to your wordpress database to make query, access tables and so on
global $wpdb;
$list_query=$wpdb->get_results("select ID, post_title from {$wpdb->posts} where post_type='slb_list' and post_status in ('draft', 'publish')");
Ex. get the post id
$post_id = $post->ID;
to read custom field value from the DB, you can use this code the wordpress function “get_post_meta”:
$first_name = (!empty(get_post_meta($post_id, 'slb_first_name', true))) ? get_post_meta($post_id, 'slb_first_name', true) : '';
Basically it takes the custom field value of that post_id and that custom field name
To take parameter value from a http request (after a submit) in PHP you have to do like this:
$first_name = ( isset($_POST['slb_first_name']) ) ? sanitize_text_field($_POST['slb_first_name']) : '';
To add a customfield, and its value, do this:
add_post_meta( $post_id, 'customfield_name', value, true/false );
The last parameter means that you want to exlude (true) or you don’t mind (false) duplicated values of that custom field for that post_id
to update a custom field:
delete_post_meta( $post_id, 'customfield_name');
Yes, all values of that custom fields will be deleted, even if you associated more than one value.
Post types are a way to categorize different types of content in WordPress.
WordPress has already the following native post type:
Most probably you just knew about the first three in the list.
And you can extend these wordpress post type with your own.
To do this you here it is a little function recap.
add_action( 'init', 'custom_posttype_def', 0 );
function custom_posttype_def()
{
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'Movies', 'Post Type General Name', 'twentytwenty' ),
'singular_name' => _x( 'Movie', 'Post Type Singular Name', 'twentytwenty' ),
'menu_name' => __( 'Movies', 'twentytwenty' ),
'parent_item_colon' => __( 'Parent Movie', 'twentytwenty' ),
'all_items' => __( 'All Movies', 'twentytwenty' ),
'view_item' => __( 'View Movie', 'twentytwenty' ),
'add_new_item' => __( 'Add New Movie', 'twentytwenty' ),
'add_new' => __( 'Add New', 'twentytwenty' ),
'edit_item' => __( 'Edit Movie', 'twentytwenty' ),
'update_item' => __( 'Update Movie', 'twentytwenty' ),
'search_items' => __( 'Search Movie', 'twentytwenty' ),
'not_found' => __( 'Not Found', 'twentytwenty' ),
'not_found_in_trash' => __( 'Not found in Trash', 'twentytwenty' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'movies', 'twentytwenty' ),
'description' => __( 'Movie news and reviews', 'twentytwenty' ),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
// You can associate this CPT with a taxonomy or custom taxonomy.
'taxonomies' => array( 'genres' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => true
);
// Registering your Custom Post Type
register_post_type( 'movies', $args );
}
There is another way to create a custom post type. It’s using the plugin Custom Post Type UI (or CPTUI). In this case you can create your post types directly in the wordpress admin with a simple UI.
Of course it depends on what you need from Custom Post Types.
If you are creating a plugin it’s better to create them inside it.
If you need them only for you personal use then it’s easer to use CPTUI.
That’s it
Headquarter
BELGIUM
Avenue Général Médecin Derache, 56
1050 – Ixelles
ITALY
Via Antonio Demarinis, 20/A
70021 – Acquaviva delle Fonti (BA)