Skip Navigation

[Resolved] Parent selection field, display post ID instead of titles in select

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a Form that allows Users to create child posts. The Form contains a select field that allows the User to select a parent post in a one-to-many relationship. Instead of displaying the parent post title in each select field option, I would like to display the post ID. Or, I would like to automate the parent post title so that the title is the same as the post ID. Parent posts are also created by Forms.

Solution: If you want to automatically set the parent post's title to be the same as its ID you could use the cred_save_data hook with your parent post Form. Remove the title field from the parent post Form first to prevent the User from submitting a title. Then use the cred_save_data hook and the wp_update_post API to programmatically set the post title.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 4 years, 1 month 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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by nicolaS-3 4 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1518323

Hi,
I have a post form that allows to select a parent (one to many) while adding children. By default the parent selection select shows the post titles of parent, that is ok in most cases, but in this particular case I'd need post-ids to be listed in the select. Is there any way to do it ? alternatively is there a way to automatically set post-title=post-id when the parent post is created (with another form)?
Hope this is clear ....
Thanks
Regards
Nicola

#1518731

Hi, if you want to automatically set the parent post's title to be the same as its ID you could use the cred_save_data hook with your parent post Form. Remove the title field from the parent post Form first to prevent the User from submitting a title. Then use the cred_save_data hook and the wp_update_post API to programmatically set the post title. We have documentation for the cred_save_data hook available here: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

Here's an example:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==12345)
  
{ 
$my_post = array(
      'ID'           => $post_id,
      'post_title' => $post_id
  );
 
// Update the post into the database
  wp_update_post( $my_post );  
}
}

If you want to display the post ID instead of the existing post title, there's no public API available for modifying the titles in the parent post dropdown. You would have to use a generic field and populate it with the results of a View. Let me know if you need more information about that.

#1525317

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.