使用帖子元中的post_id链接两个不同的帖子

时间:2017-09-28 作者:inrsaurabh

我有一个前端表单,用户可以通过它创建自定义帖子(在我的例子中是事件)。

现在我想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

}

1 个回复
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 );

}

结束

相关推荐

Single必须使用Plugins目录进行本地开发

我有多个本地安装的WordPress,并希望有一个单一的必须使用插件目录为我的所有本地网站。有什么我可以添加到wp配置,例如,让我有一个文件夹,可以用于我的所有网站?或者另一种方法?例如:/根/站点/站点1//根/站点/站点2//根/站点/站点3/。。。所有用途:/根/mu插件/谢谢