Skip Navigation

[Resolved] Update custom field with data from elsewhere when submitting a post

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

Problem:
We want to populate a Toolset Custom Field with data from the post (author, or else) when a post is edited or created.

How can we do that?

Solution:
With a save_post or cred_save_data action of Toolset and WordPress hooking custom PHP code the event of saving a post, be it in the backend or front end.

Inside these hooks you can use native WordPress method update_post_meta for example, to update the Toolset Custom Fields.

It is important here to know that the meta key of a Toolset Field always will start with wpcf-

Relevant Documentation:
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 4 replies, has 2 voices.

Last updated by WillL606 4 years, 8 months ago.

Assisted by: Beda.

Author
Posts
#1550339

Tell us what you are trying to do?

Change the output wp Author field to my own field for a custom post type. This is for Dublin Core metadata. The author currently selects the admin or post author not the author of a published essay.

Is there any documentation that you are following?

No

Is there a similar example that we can see?

I used this snippet - no success:

// Change WP Author to custom field

add_filter( 'the_author', 'guest_author_name' );
add_filter( 'get_the_author_display_name', 'guest_author_name' );

function guest_author_name( $name ) {
global $post;

$author = get_post_meta( $post->ID, 'essay-author', true );

if ( $author )
$name = $author;

return $name;
}

What is the link to your site?

hidden link

#1550501

This is custom code, which we can not help with, as our Support Policy states:
https://toolset.com/toolset-support-policy/

We can help with such questions when it, for example, is done in a Toolset Form.
For Toolset Forms, Toolset provides an API that allows changing, with code, any value you want to any value you want at different stages of a Form's life (submit, success, redirect, etc etc)
https://toolset.com/documentation/programmer-reference/cred-api/

But, the code within those hooks provided by Toolset is custom code, which we rarely can help with.

Now, if you do not use Toolset Forms, you could, for example, use the WordPress native hook save_post(), which is fired (just like Toolset Form's API) in the stage of saving a post in the backend.
This is WordPress core code API, hence it can be studied in the Codex:
https://developer.wordpress.org/reference/hooks/save_post/

Within that hook, you could do any operation such as updating fields with author data, or author data with fields values, or else, just like in a Toolset Forms Hook.
The only special related to Toolset, in this case, is the key of the post fields will have a wpcf- prefix for their meta key.
So if you have a Custom Field named "my-field", in the database it will be wpcf-my-field"

This will be important when working with get_post_meta for example so you can get the values of the right field or update them

I see for example you use get_post_meta, with the field slug "essay-author". If this is a Toolset Custom Field, you would need to use "wpcf-essay-author" as the slug of the field.

I hope the above information helps to proceed, please also have a look at my Pastebin https://pastebin.com/u/bedas for raw examples, our DOC in the sections "More - usage example" also has many code examples, and this search here lists several raw snippets to:
hidden link
This can help if you need some examples or inspiration, but it's not part of the Support provided - the examples are just for reference.

#1550943

Thanks for this, but I am little lost with the various php snippets. I understand that you can't help with general hooks, but this is the problem and maybe you can let me know of it is possible:

I have a Toolset Custom Post Type - Essays. For that I also have a Toolset custom field "essay-author". This allows be to assign an author name that is NOT the blog post author who simply pastes the code of the essay and publishes. But I am trying to get the metadata for an essay post to reflect the essay author, not the WordPress author who publishes the essay post. I have a Dublin Core Metadata plugin that grabs the WP metadata for title, author, description. I would like to find a hook, that will take the custom fields I set up with Toolset and replace the standard WP fields. Is that possible?

#1553229

This is possible, as earlier described, in save_post or cred_save_data hooks of Toolset and WordPress.

I am not sure if the other plugin offers some hooks when the post is saved, then you can use those. The syntax to get the fields is the same as for any other Custom Field in WordPress and as earlier described the only special thing is the prefix added by Toolset: wpcf-
You'd get fields with get_post_meta() and use wp_update_post to update some post data with it

The Docs for these two functions can be found here https://developer.wordpress.org/reference/functions/get_post_meta, https://developer.wordpress.org/reference/functions/wp_update_post/

I hope this helps!

#1556873

My issue is resolved now. Thank you!