I'm using Zapier to create custom posts. Upon WordPress creating a custom post from Zapier I would like it to populate a custom field with the post title so I can then create a view that can have a drop down select based on the the custom field (ie. post title) values.
Could you point me in the right direction as to how to create a filter? For reference the post type is practice-point and the custom field slug is wpcf-practice-point-title.
Regards
Hello, you can use the WordPress API save_post to automatically set a custom field value when the post is created by a 3rd-party system. I worked on a similar ticket recently and I have some custom code you can use. Previous ticket with some additional details for you: https://toolset.com/forums/topic/set-the-value-of-a-toolset-custom-field-when-a-woocommerce-order-is-created/
// https://toolset.com/forums/topic/populate-custom-field-with-post-title/
// Automatically set practice point custom field value to match practice point post title
function tssupp_set_pp_title ( $post_id, $post, $update ) {
$field_slug = 'practice-point-title';
$post_type_slug = 'practice-point';
// - bail on wrong post type or on update
$post_type = get_post_type( $post_id );
if ( $post_type !== $post_type_slug || $update ) {
return;
}
update_post_meta( $post_id, 'wpcf-' . $field_slug, get_the_title( $post_id ) );
}
add_action( 'save_post', 'tssupp_set_pp_title', 100, 3);
You shouldn't need to make any modifications here. Note that this code only sets the custom field value when the post is initially created. It will not update the field value if the post is modified in the future.
Hi Christian,
Thanks for your reply. Unfortunately with that code it has caused and new entries to pre-populate the field with Auto Draft. It doesn't matter if I create a post via Zapier or manually from wordpress admin. Weird.
Thoughts?
Okay maybe the initial draft is auto-saved before the post title is set. I can remove the update check so that the custom field is updated whenever the post is updated:
// https://toolset.com/forums/topic/populate-custom-field-with-post-title/
// Automatically set practice point custom field value to match practice point post title
function tssupp_set_pp_title ( $post_id, $post, $update ) {
$field_slug = 'practice-point-title';
$post_type_slug = 'practice-point';
// - bail on wrong post type or on update
$post_type = get_post_type( $post_id );
if ( $post_type !== $post_type_slug ) {
return;
}
update_post_meta( $post_id, 'wpcf-' . $field_slug, get_the_title( $post_id ) );
}
add_action( 'save_post', 'tssupp_set_pp_title', 100, 3);
Try that and see if it resolves the auto draft problem.
I'm sorry Christian, I'm still getting Auto Draft with that code
Not sure offhand, I will need to take a closer look. Unfortunately my day is closing here and I'm out until Sunday. If you'd like to leave login credentials and FTP logins here for me, I will be glad to jump back on this ticket Sunday. I have activated private reply fields here. If you'd prefer to try to get someone else to take a look sooner, feel free to start another ticket and you will be added to the queue again to be taken in turn.
Okay I updated the code as follows to work when creating a post in wp-admin:
function tssupp_set_pp_title ( $post_id, $post, $update ) {
$field_slug = 'practice-point-title';
$post_type_slug = 'practice-point';
// - bail on wrong post type or on pre-publish auto draft title
$post_type = get_post_type( $post_id );
$post_title = get_the_title( $post_id );
if ( ( $post_type !== $post_type_slug ) || ( $post_title == 'Auto Draft') ) {
return;
}
update_post_meta( $post_id, 'wpcf-' . $field_slug, get_the_title( $post_id ) );
}
add_action( 'save_post', 'tssupp_set_pp_title', 100, 3);
However, I'm not sure how this would work with Zapier and I'm not familiar enough with their system to be able to test it. Perhaps their support staff can provide a working example of setting some standard custom field value using the post title? If you can show me some example code of setting any standard WordPress custom field value using the post title in Zapier, I will update that code to work with a field created in Types.
Absolutely spot on, works a treat! Thank you Christian. Hope it works for other people too.
My issue is resolved now. Thank you!