Skip Navigation

[Closed] Creating Post in CPT from Gravity Form

This support ticket is created 4 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Tagged: 

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 4 years, 3 months ago.

Author
Posts
#1796851

Tell us what you are trying to do?

I'm trying to re-create an action that was happening on a previous instance of my site that didn't use Toolset, but make it work with the plugin. Basically, I have a custom post type "members", and i'd like to automatically create a post and fill out the custom fields whenever somebody fills in a gravity form to 'apply for membership', which then uses the details in that form to fill out the custom fields and post title.

In the previous instance, the code worked like this:

add_action('gform_after_submission_16', 'create_drafted_member', 10, 2);
function create_drafted_member($entry, $form) {

  $member = [
    'post_title'     => $entry["62"] . ' ' . $entry["13"] . ' ' . $entry["11"],
    'post_status'    => 'draft',
    'post_type'      => 'member',
    'comment_status' => 'closed',
  ];

  $member_meta = [
    '_member_details_title'             => $entry["62"],
    '_member_details_first_name'        => $entry["13"],
    '_member_details_surname'           => $entry["11"],
    '_member_details_nickname'          => $entry["12"],
    '_member_details_id_number'         => $entry["14"],
    '_member_details_nationality'       => $entry["16"],
    '_contact_details_email'            => $entry["24"],
    '_contact_details_mobile'           => $entry["23"],
    '_contact_details_telephone'        => $entry["20"],
    '_contact_details_telephone_home'   => $entry["21"],
    '_contact_details_fax'              => $entry["22"],
    '_contact_details_website'          => $entry["25"],
    '_contact_details_physical_address' => $entry["36"],
    '_contact_details_postal_address'   => $entry["41"],
  ];

  // Insert the post into the database
  $post_id = wp_insert_post($member);

  foreach ($member_meta as $meta_key => $meta_value) {

    update_post_meta($post_id, $meta_key, $meta_value);
  }


}

Would this same function work with Toolset by just replacing the fields like '_member_details_title' with the slugs i have set up in Toolset? Also, the one other function i need to add to make it so that it works with an existing view query is to edit it so that the Post Author is set as the currently logged in user for the newly-created post, is that possible?

If you need any more information please let me know.

#1797621

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

When you set up custom post types with Types they are just regular WordPress custom post types, so there is no reason why you cannot use standard WordPress functions likje wp_insert_post to create such posts programmatically.

If you are using Types custom fields these are stored in wp_postmeta much like other custom fields, the one proviso being that Types stores fields with a 'wpcf-' prefix. So if you use Types to register a custom field "contact_email" it would be stored in wp_postmeta with a key of 'wpcf-contact_email', and that's the key you would need to use when programmatically saving or retrieving the fields with update_post_meta or get_post_meta.

If you want to set the id of the post author when you insert the post you can get the current user id using get_current_user_id() (https://developer.wordpress.org/reference/functions/get_current_user_id/)

The topic ‘[Closed] Creating Post in CPT from Gravity Form’ is closed to new replies.