I think that that there is no need to create another table for restaurant, but I don’t know the entire structure of your site.
Once Restaurants are already a CPT, they are saved into posts table, and with information you share that seems enough.
Autocreate a Restaurant post when a Company post is created and the restaurant name field is filled, is very easy without any plugin.
If you insert restaurant name/description using a metabox, probably you are hooking save_post
to save that fields to restaurant post, you can use same function to save restaurant post using wp_insert_post
: using the restaurant name as title, the restaurant description as content and setting post_author
to the same post_author
of company post. In the restaurant post, you can save a custom field, something like '_company_ID'
and set it to the company post id it will be super useful when you need to retrieve the restaurant linked to a specific company, in addition it will help you to avoid duplicates.
I will assume you have a metabox to insert restaurant meta fields.
If not, I suggest to add one, see add_meta_box
docs.
Let’s assume you have a function hooking 'save_post'
to save restaurant meta fields from the metabox:
add_action('save_post', 'save_restaurant_fields');
function save_restaurant_fields( $postid ) {
/* If this is an autosave, do not do anything */
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
/*
Check if our nonce is set (of course you have to set a nonce in the metabox)
see wp_nonce_field in codex //codex.wordpress.org/Function_Reference/wp_nonce_field
*/
$nonce = filter_input(INPUT_POST, 'restaurant_data_nonce', FILTER_SANITIZE_STRING);
if ( empty($nonce) || ! wp_verify_nonce( $nonce, 'restaurant_data' ) ) return;
/* Check the user's permissions */
if ( ! current_user_can( 'edit_post', $postid ) ) return;
/* Continue only for Company CPT */
$post = get_post( $postid );
if ( $post->post_type !== 'company' ) return;
/* Get & sanitize user input */
$rname = filter_input(INPUT_POST, 'restaurant_name', FILTER_SANITIZE_STRING);
$rdesc = filter_input(INPUT_POST, 'restaurant_desc', FILTER_SANITIZE_STRING);
/* Update the meta fields in the database */
update_post_meta( $postid, '_restaurant_name', $rname );
update_post_meta( $postid, '_restaurant_desc', $rdesc );
/* Now check if exists a restaurant for this company */
$meta = array( array( 'key' => '_company_ID', 'value' => $post->ID ) );
$exists = new WP_Query( 'post_type' => 'restaurant', 'meta_query' => $meta );
if ( ! $exists->found_posts ) { /* restaurant for company does not exists */
$restaurant = array(
'post_type' => 'restaurant',
'post_title' => $rname,
'post_content' => $rdesc,
'post_author' => $post->post_author,
'post_status' => 'publish'
);
$rid = wp_insert_post( $restaurant ); /* save restaurant post */
if ( is_numeric($rid) && (int) $rid > 0 ) {
/* save company ID in a restaurant meta field */
update_post_meta( $rid, '_company_ID', $post->ID );
/*
I inserted restaurant post name/description as post title/description
but if you need restaurant them also in custom fields
*/
update_post_meta( $rid, '_restaurant_name', $rname );
update_post_meta( $rid, '_restaurant_desc', $rdesc );
}
}
}
This code is fine if a company can have only one related restaurant, otherwise I suggest to add to the metabox a unique identifier for restaurants, because if you rely on restaurant name, you will not able to rename it, neither if a typo happen…