The idea is to create a plugin that will add a custom post but all the metadata of this custom post should be in a custom table. Invisible for the user and customizable. I’ve tried to follow this.
We create a new custom type
// Register Custom Post Type
function custom_darkink_film() {
$labels = array(
'name' => _x( 'Films', 'Post Type General Name', 'darkink_film_domain' ),
'singular_name' => _x( 'Film', 'Post Type Singular Name', 'darkink_film_domain' ),
'menu_name' => __( 'Films', 'darkink_film_domain' ),
'name_admin_bar' => __( 'Films', 'darkink_film_domain' ),
'archives' => __( 'Film Archives', 'darkink_film_domain' ),
'attributes' => __( 'Film Attributes', 'darkink_film_domain' ),
'parent_item_colon' => __( 'Parent film:', 'darkink_film_domain' ),
'all_items' => __( 'All films', 'darkink_film_domain' ),
'add_new_item' => __( 'Add New film', 'darkink_film_domain' ),
'add_new' => __( 'Add Film', 'darkink_film_domain' ),
'new_item' => __( 'New Film', 'darkink_film_domain' ),
'edit_item' => __( 'Edit Film', 'darkink_film_domain' ),
'update_item' => __( 'Update Film', 'darkink_film_domain' ),
'view_item' => __( 'View Film', 'darkink_film_domain' ),
'view_items' => __( 'View Films', 'darkink_film_domain' ),
'search_items' => __( 'Search Film', 'darkink_film_domain' ),
'not_found' => __( 'Film not found', 'darkink_film_domain' ),
'not_found_in_trash' => __( 'Film not found in Trash', 'darkink_film_domain' ),
'featured_image' => __( 'Featured Image', 'darkink_film_domain' ),
'set_featured_image' => __( 'Set featured image', 'darkink_film_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'darkink_film_domain' ),
'use_featured_image' => __( 'Use as featured image', 'darkink_film_domain' ),
'insert_into_item' => __( 'Insert into film', 'darkink_film_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this film', 'darkink_film_domain' ),
'items_list' => __( 'Films list', 'darkink_film_domain' ),
'items_list_navigation' => __( 'Films list navigation', 'darkink_film_domain' ),
'filter_items_list' => __( 'Filter films list', 'darkink_film_domain' ),
);
$args = array(
'label' => __( 'Film', 'darkink_film_domain' ),
'description' => __( 'a shot film', 'darkink_film_domain' ),
'labels' => $labels,
'supports' => array( 'title' ),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'post_darkink_film', $args );
}
add_action( 'init', 'custom_darkink_film', 0 );
We create the database
$darkink_test_db_version = '1.2';
// Create the Custom Table
function database_darkink_film(){
global $wpdb;
global $darkink_test_db_version;
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . "darkink_film";
$sql = "CREATE TABLE $table_name (
post_id bigint(20) NOT NULL,
director varchar(255) NOT NULL,
writer varchar(255) NOT NULL,
year year(4) NOT NULL,
PRIMARY KEY (post_id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
add_option( 'darkink_test_db_version', $darkink_test_db_version );
}
register_activation_hook( __FILE__, 'database_darkink_film' );
Now we add all the handling for the custom table
function darkink_film_meta_insert_post( $post_id, $post, $udpate ) {
global $wpdb;
if ( get_post_type( $post ) == 'post_darkink_film' &&
$post->post_status != 'trash' &&
!$udpate &&
$wpdb->get_var( $wpdb->prepare("SELECT post_id FROM " . $wpdb->prefix . "darkink_film WHERE post_id = %d", $post_id )) == null
) {
$wpdb->insert( $wpdb->prefix . "darkink_film", array( 'post_id' => $post_id ) );
}
}
function darkink_film_meta_delete_post( $post_id ) {
global $wpdb;
if ( get_post_type( $post_id ) == 'post_darkink_film' ) {
$wpdb->query( $wpdb->prepare("DELETE FROM " . $wpdb->prefix . "darkink_film WHERE post_id = %d", $post_id ) );
}
}
function darkink_film_meta_update( $check, $post_id, $meta_key, $meta_value ) {
global $wpdb;
if ( get_post_type( $post_id) == 'post_darkink_film' && in_array( $meta_key, array( 'director', 'writer', 'year' ) ) ) {
return $wpdb->update(
$wpdb->prefix . "darkink_film",
array( $meta_key => maybe_serialize( $meta_value ) ),
array( 'post_id' => $post_id)
);
} else {
return $check;
}
}
function darkink_film_meta_delete( $check, $object_id, $meta_key ) {
global $wpdb;
if ( get_post_type( $post ) == 'post_darkink_film' && in_array( $meta_key, array( 'director', 'writer', 'year' ) ) ) {
return $wpdb->update(
$wpdb->prefix . "darkink_film",
array( $meta_key => null ),
array( 'post_id' => $object_id )
);
} else {
return $check;
}
}
function darkink_film_meta_get( $check, $post_id, $meta_key ) {
if ( get_post_type( $post_id) == 'post_darkink_film' && in_array( $meta_key, array( 'director', 'writer', 'year' ) ) ) {
$result = $wpdb->get_var( $wpdb->prepare("SELECT $meta_key FROM " . $wpdb->prefix . "darkink_film WHERE post_id = %d", $post_id) );
return maybe_unserialize( $result );
} else {
return $check;
}
}
add_action( 'wp_insert_post', 'darkink_film_meta_insert_post', 10, 3 );
add_action( 'delete_post', 'darkink_film_meta_delete_post', 10 );
add_filter( 'add_post_metadata', 'darkink_film_meta_update', 0, 4 );
add_filter( 'update_post_metadata', 'darkink_film_meta_update', 0, 4 );
add_filter( 'delete_post_metadata', 'darkink_film_meta_delete', 0, 3 );
add_filter( 'get_post_metadata', 'darkink_film_meta_get', 0, 3 );
We need a lot of code to make it work….