Custom post type "Company" has:

  • Restaurant name field

  • Restaurant description field

  • ..and other more fields

Custom post type "Restaurant" has:

  • Restaurant name field

  • Restaurant description field

How can I make it so that whenever I add a new Company (the restaurant fields are filled out), it will automatically add a new Restaurant whose fields/details are the same with the newly added Company?

My idea is:

  • Whenever I create a new Company, I will add the restaurant details to a "restaurants" table in the database

  • Also, whenever I create a new company, I will get the latest entry from "restaurants" table and somehow append them to create a new Restaurant post

Is this even possible? If not or if you have another idea to solve the problem, please share.

Feel free to suggest multiple plugins if they manage to get the job done.

Providing an example would be extremely helpful. Thanks!

1 Answer 1


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...

  • Thank you! I have questions, though. In this part /* Get & sanitize user input */ I assume this part of the code will get the restaurant name and restaurant description metabox values from the Company post type? Commented Dec 10, 2013 at 9:19
  • Also, in this part /* Update the meta fields in the database */ which am I updating, the Company post type or Restaurant post type? Commented Dec 10, 2013 at 9:19
  • If I understood everything, here's what you did: Every time a Company post is made, I will get the restaurant name and restaurant description. I will then check if a company_id is equal to a restaurant_id if not, I will insert the restaurant name and restaurant description to the database with its id equal to the company_id (but I noticed in your code you didn't mention the Restaurant post type?). Commented Dec 10, 2013 at 9:31
  • (1) Everytime a restaurant is made or updated, the restauran name / description is also updated according to values in the metabox (2) I check if a exists a restaurant with a custom field called '_company_ID' that is equal to current comapny, if so do nothing, otherwise create it also setting the '_company_ID' field. And you're right, I forgot post type argument in the code, now added @user2772219
    – gmazzap
    Commented Dec 10, 2013 at 9:39
  • Thanks a lot man! I will test the code when I get home and get back to you. Commented Dec 10, 2013 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.