我正在寻找一种在发布自定义帖子类型时自动插入自定义字段的方法。我反复地从这里找到了同样的解决方案:
http://pippinspages.com/tutorials/publish-action-hook-for-custom-post-types/
我试过这个,但出于某种原因,它对我不起作用。我有一个名为sales\\u pages的自定义帖子类型
这是我的代码:
// function to be executed when a custom post type is published
function run_when_post_published($post_ID)
{
// your function code here
global $wpdb;
if(!wp_is_post_revision($post_ID)) {
add_post_meta($post_ID, \'field-name\', \'custom value\', true);
}
}
// replace {custom_post_type_name} with the name of your post type
add_action(\'new_to_publish_sales_pages\', \'run_when_post_published\');
add_action(\'draft_to_publish_sales_pages\', \'run_when_post_published\');
add_action(\'pending_to_publish_sales_pages\', \'run_when_post_published\');
你能告诉我这有什么问题吗?
我已经用常规帖子证实了这一点(当然是用帖子发布挂钩)。它只是对我的自定义帖子类型做了不同的处理。
提前感谢您的建议。
干杯,布莱恩
SO网友:Hameedullah Khan
正如Otto所说,没有${old\\u status}to${new\\u status}{uu${post\\u type}操作挂钩,但可以使用new\\u to\\u publish、draft\\u to\\u publish和pending\\u to\\u publish在自定义帖子上添加帖子元。
// function to be executed when a custom post type is published
function run_when_post_published($post)
{
// your function code here
global $wpdb;
if(!wp_is_post_revision($post->ID) && $post->post_type == \'sales_pages\') {
add_post_meta($post->ID, \'field-name\', \'custom value\', true);
}
}
// replace {custom_post_type_name} with the name of your post type
add_action(\'new_to_publish\', \'run_when_post_published\');
add_action(\'draft_to_publish\', \'run_when_post_published\');
add_action(\'pending_to_publish\', \'run_when_post_published\');
SO网友:MZAweb
你应该钩住post_save
检查几件事:
这是您的自定义帖子类型,它不是自动保存,也不是修订版,类似于:
add_action(\'save_post\', \'my_custom_save\');
function my_custom_save( $post_id ){
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE )
return $post_id;
if ( \'your_custom_post_type\' == $_POST[\'post_type\'] ){
if (!wp_is_post_revision($post_id)){
add_post_meta($post_ID, \'field-name\', \'custom value\', true);
}
}
}