SAVE_POST_TYPE上缺少自定义POST类型元字段

时间:2018-04-14 作者:Jonathan Stegall

我维护了一个插件,当创建、更新和删除匹配的对象时,它会将选定的WordPress数据发送到外部REST API(本例中为Salesforce)。

例如post 工作原理如下:

add_action( \'save_post\', array( $this, \'post_actions\' ), 11, 2 );
对于自定义帖子类型,其工作原理如下:

add_action( \'save_post_\' . $object_type, array( $this, \'post_actions\' ), 11, 2 );
post_actions 是一种尝试检索所有WordPress字段数据(可以包括自定义字段)然后将其发送到API的方法。

例如,标准的post类型结果如下所示:

首先,它是一个草稿,没有元字段:

[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
然后它与元字段一起发布:

[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] => [email protected]
[_payment_type] => 
[RecordTypeId] => 
[_edit_lock] => 1523674964:1
之后我的日志似乎会被截断,但一旦文章从草稿更改为发布,自定义字段值就会出现。我的插件忽略草稿,这样它就不会在草稿上失败。但在任何情况下,都会将数据发送给Salesforce,并创建一个没有错误的项目;必填字段都按其应有的方式存在。

当我对自定义post对象(称为opportunity)执行同样的操作时,它是这样产生的:

第一次,它是一个草稿:

[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] =>     
第二,出版:

[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] => 
WordPress中的此自定义帖子类型具有与标准相同的自定义字段post 类型,但Salesforce无法创建/更新记录,因为必填字段没有值。

我一直无法找到这两个钩子应该如何工作的差异来解释这一点。是否有任何方法可以确保我能够加载所有的元数据来发送它们?

1 个回复
最合适的回答,由SO网友:Jonathan Stegall 整理而成

一时兴起,我尝试了以下方法:停止检查自定义post对象,只需使用save_post. 我可以从save_post 不使用save_post_type. 也就是说,我可以运行以下命令:

add_action( \'save_post\', array( $this, \'post_actions\' ), 11, 2 );
而不是只在post类型为post, 我可以运行它,只要它不是user, attachment, tag, post_tag, category, 或comment.

所以它并没有真正回答为什么save_postsave_post_type 在哪些数据可用方面表现不同,但它确实解决了我丢失数据的直接问题。

结束

相关推荐

列出分类法:如果分类法没有POST,就不要列出分类法--取决于定制的POST-META?

这可能很难解释,我不知道是否有解决办法!?我有一个名为“wr\\u event”的自定义帖子类型和一个名为“event\\u type”的分层自定义分类法。自定义帖子类型有一个元框,用于event_date 并且与此帖子类型关联的所有帖子都按以下方式排序event_date. 我在循环中有一个特殊的条件来查询event_date 已经发生了-在这种情况下,它没有显示,但只列在我的档案中。就像你可以使用wp_list_categories() 我编写了一个自定义函数,它以完全相同的方式列出所有分类术语。现在