0

I maintain a plugin that sends selected WordPress data to an external REST API (Salesforce, in this case) when matching objects are created, updated, deleted.

For example, post works like this:

add_action( 'save_post', array( $this, 'post_actions' ), 11, 2 );

For custom post types, it works like this:

add_action( 'save_post_' . $object_type, array( $this, 'post_actions' ), 11, 2 );

post_actions is a method that attempts to retrieve all of the WordPress field data (which can include custom fields), and then sends it to the API.

For example, a standard post type result is like this:

First, it comes through as a draft without the meta fields:

[ID] => 600
[post_author] => 1
[post_date] => 2018-04-14 03:02:29
[post_date_gmt] => 0000-00-00 00:00:00
[post_content] => 
[post_title] => test post
[post_excerpt] => 
[post_status] => draft
[comment_status] => open
[ping_status] => open
[post_password] => 
[post_name] => 
[to_ping] => 
[pinged] => 
[post_modified] => 2018-04-14 03:02:29
[post_modified_gmt] => 2018-04-14 03:02:29
[post_content_filtered] => 
[post_parent] => 0
[guid] => https://vanilla-wordpress.test/?p=600
[menu_order] => 0
[post_type] => post
[post_mime_type] => 
[comment_count] => 0
[_close_date] => 
[_stage] => 
[_type] => 
[_subtype] => 
[_amount] => 
[_first_name] => 
[_last_name] => 
[_email_address] => 
[_payment_type] => 
[RecordTypeId] => 
[_edit_lock] => 1523674949:1
[_edit_last] => 1

Then it comes through as published with the meta fields:

[ID] => 600
[post_author] => 1
[post_date] => 2018-04-14 03:02:51
[post_date_gmt] => 2018-04-14 03:02:51
[post_content] => test
[post_title] => test post
[post_excerpt] => 
[post_status] => publish
[comment_status] => open
[ping_status] => open
[post_password] => 
[post_name] => test-post-6
[to_ping] => 
[pinged] => 
[post_modified] => 2018-04-14 03:02:51
[post_modified_gmt] => 2018-04-14 03:02:51
[post_content_filtered] => 
[post_parent] => 0
[guid] => https://vanilla-wordpress.test/?p=600
[menu_order] => 0
[post_type] => post
[post_mime_type] => 
[comment_count] => 0
[_close_date] => 04/13/2018
[_stage] => Closed Won
[_type] => Donation
[_subtype] => Donation: Individual
[_amount] => 199
[_first_name] => test
[_last_name] => name
[_email_address] => test@test.com
[_payment_type] => 
[RecordTypeId] => 
[_edit_lock] => 1523674964:1

My log seems to truncate after that, but the custom field values are there once the post is changed from draft to publish. My plugin ignores drafts, so that it doesn't fail on the draft makes sense. But in any case, the data is sent to Salesforce and an item is created with no errors; the required fields are all there as they should be.

When I do the same thing with a custom post object (it's called opportunity), it comes through like this:

The first time, it is a draft:

[ID] => 597
[post_author] => 1
[post_date] => 2018-04-14 03:00:33
[post_date_gmt] => 0000-00-00 00:00:00
[post_content] => 
[post_title] => test
[post_excerpt] => 
[post_status] => draft
[comment_status] => closed
[ping_status] => closed
[post_password] => 
[post_name] => 
[to_ping] => 
[pinged] => 
[post_modified] => 2018-04-14 03:00:33
[post_modified_gmt] => 2018-04-14 03:00:33
[post_content_filtered] => 
[post_parent] => 0
[guid] => https://vanilla-wordpress.test/?post_type=donation&p=597
[menu_order] => 0
[post_type] => donation
[post_mime_type] => 
[comment_count] => 0
[_edit_lock] => 1523674833:1
[_edit_last] => 1
[_close_date] => 
[_stage] => 
[_type] => 
[_subtype] => 
[_amount] => 
[_payment_type] => 
[_first_name] => 
[_last_name] => 
[_email_address] =>     

And the second, it is published:

[ID] => 597
[post_author] => 1
[post_date] => 2018-04-14 03:00:54
[post_date_gmt] => 2018-04-14 03:00:54
[post_content] => test
[post_title] => test
[post_excerpt] => 
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] => 
[post_name] => test-4
[to_ping] => 
[pinged] => 
[post_modified] => 2018-04-14 03:00:54
[post_modified_gmt] => 2018-04-14 03:00:54
[post_content_filtered] => 
[post_parent] => 0
[guid] => https://vanilla-wordpress.test/?post_type=donation&p=597
[menu_order] => 0
[post_type] => donation
[post_mime_type] => 
[comment_count] => 0
[_edit_lock] => 1523674848:1
[_edit_last] => 1
[_close_date] => 
[_stage] => 
[_type] => 
[_subtype] => 
[_amount] => 
[_payment_type] => 
[_first_name] => 
[_last_name] => 
[_email_address] => 

This custom post type in WordPress has identical custom fields as the standard post type, but Salesforce fails to create/update records because the required fields have no values.

I've been unable to find a difference in how these two hooks should work that would account for this. Is there any way I can ensure that I'm able to load all of the meta values to send them?

  • the debug of $post that you show is done at the start of the hook save_post_... ? – mmm Apr 14 '18 at 4:56
  • @mmm yes. I run a sql method to get the distinct meta keys, and then it tries to get their values from the $post object. – Jonathan Stegall Apr 14 '18 at 21:25
0

On a whim I tried this: stop checking for a custom post object and just use save_post. I'm able to get the data I need from save_post without using save_post_type. That is, I can run this:

add_action( 'save_post', array( $this, 'post_actions' ), 11, 2 );

And instead of only running that when the post type is post, I can run it as long as it is not user, attachment, tag, post_tag, category, or comment.

So it doesn't really answer the question of why save_post and save_post_type behave differently with regard to what data is available, but it does solve my immediate issue of my missing data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.