我有一个前端表单,用户可以通过它创建自定义帖子(在我的例子中是事件)。
现在我想fetch all address data
并在用户从前端提交完整表单后创建新帖子。
我正在尝试使用save\\u post在中触发我的函数function.php
.
但下面的函数给出500 error in console
页面被冻结。
add_action( \'save_post\',\'add_menu_custom_event\');
function add_menu_custom_event($post_id) {
//Check it\'s not an auto save routine
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE )
return;
//Perform permission checks! For example:
if ( !current_user_can(\'edit_post\', $post_id) )
return;
echo "post_id".$post_id;
$data = array (
\'post_title\' => \'New Title6\',
\'post_type\' => \'tribe_venue\',
\'meta_input\' => array (
\'_VenueAddress\' => \'Address check\',
\'_VenueCity\' => \'city check\',
\'_VenueCountry\' => \'Afghanistan\',
\'_VenueProvince\' => \'province\',
\'_VenueZip\' => \'121007\',
\'_VenuePhone\' => \'7503118112\',
\'_VenueURL\' => \'someurl.com\',
\'_VenueShowMapLink\' => 1,
),
);
// save the new post
$pid = wp_insert_post($data,true); // gives 1536 _EventVenueID
update_post_meta( $post_id, \'_EventVenueID\', $pid ); // i am trying to link parent post id to its child post id
}
SO网友:inrsaurabh
我有问题,这是我的答案。
现在两个帖子链接正确。我只需要动态更新元数据。
add_action( \'save_post\',\'add_menu_custom_event\', 10, 3);
function add_menu_custom_event( $post_id, $post, $update ) {
/*
* In production code, $slug should be set only once in the plugin,
* preferably as a class property, rather than in each function that needs it.
*/
//Check it\'s not an auto save routine
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE )
return;
//Perform permission checks! For example:
if ( !current_user_can(\'edit_post\', $post_id) )
return;
//if( false != $update )
//d(get_current_screen( ));
/d($update);
//die;
$post_type = get_post_type($post_id);
$_REQUEST[\'\']
// If this isn\'t a \'book\' post, don\'t update it.
if ( "tribe_events" != $post_type ) return;
$data = array (
\'post_title\' => \'New Title6\'.rand(1, 9999),
\'post_type\' => \'tribe_venue\',
\'meta_input\' => array (
\'_VenueAddress\' => \'Address check\',
\'_VenueCity\' => \'city check\',
\'_VenueCountry\' => \'Afghanistan\',
\'_VenueProvince\' => \'province\',
\'_VenueZip\' => \'121007\',
\'_VenuePhone\' => \'7503118112\',
\'_VenueURL\' => \'someurl.com\',
\'_VenueShowMapLink\' => 1,
),
);
$pid = wp_insert_post($data,true);
update_post_meta( $post_id, \'_EventVenueID\', $pid );
}