Skip Navigation

[Resolved] How to update post excerpt field by custom field when I submit form?

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

Problem:
How could I update a Post Excerpt when submitting a Toolset Form, using the value of a Custom Field set in the Toolset Form?

Solution:

Excerpts are built using the Post Body and are part of the Post, hence you will need to manipulate that content with wp_update_post(), which is a WordPress API Function.

To do this when submitting a Toolset Form, you can update your post within a cred_save_data() hook, where you should define a $variable with the content of the Custom Field from which you want to pull the data to update the Excerpt with.

TIPP:
You can get that Field's contents either from $_POST or, you can get it with WordPress API get_post_meta()

Then, the $variable holding the value set in the Custom Field on the Form, can be used to update the Post Excerpt using wp_update_post() within the Toolset Forms Hook

Here is an example of such custom code that takes a custom field value and updates the Post Title with it:
https://pastebin.com/2BTbVbcs

To use it for Post Excerpt, you'd just target post_excerpt, instead of post_title, which is used in the example linked

Relevant Documentation:

https://developer.wordpress.org/reference/functions/get_post_meta/

https://www.php.net/manual/en/reserved.variables.post.php

https://developer.wordpress.org/reference/functions/wp_update_post/

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

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

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 2 replies, has 2 voices.

Last updated by FuChan 4 years, 8 months ago.

Assisted by: Beda.

Author
Posts
#1542787

Hi Support,

I had a custom field is wpcf-student-name. And I had a form id is 346.
I want to update post excerpt field when I submit form(form had wpcf-student-name custom field).

But I read Toolset history support post and try to those code. It is not success. Could you help me? Thanks.

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
	$excerpt = get_the_excerpt();
    // if a specific form
    if ($form_data['id']==346)
    {
        if (isset($_POST['wpcf-student-name']))
        {
            // add it to saved post meta
            add_post_meta($post_id, $excerpt, $_POST['wpcf-student-name'], true);
        }
    }
}

#1542919

I suggest to consult this DOC:
https://developer.wordpress.org/reference/functions/add_post_meta/

You will see that add_post_meta is to add Post Meta Fields.
Not to add Excerpts.
Excerpts are built using the Post Body and are part of the Post, so you will need to manipulate that content with wp_update_post(), which is another WordPress API Function:
https://developer.wordpress.org/reference/functions/wp_update_post/

What you will need in your cred_save_data hook, is a $variable defined with the content of your Custom Field (which you want to use, to update the Excerpt with)
You can get that Fields' contents either from $_POST (see more information on $_POST PHP Form object here hidden link
Or, you can get it with WordPress API get_post_meta()
https://developer.wordpress.org/reference/functions/get_post_meta/

Then, that data can be used in wp_update_post() to populate anything of the post you want.
Here, for example, a code that takes a field value and updates the Post Title with it:
https://pastebin.com/2BTbVbcs
For post excerpt, you'd target the post_excerpt, instead of post_title

Let me know if you need more details for this!

#1547247

My issue is resolved now. Thank you!