publish_post 和 wp_insert_post 的无限循环错误

Infinite loop error with publish_post and wp_insert_post

我遇到了无限循环错误。当为默认英语语言创建 post 时,我需要在德语中插入 post。 我使用 publish_post 动作挂钩来捕捉英语 posting 事件。但是 publish_post 挂钩也会在创建德语 post 时由 wp_insert_post() 函数执行。所以无限错误发生了。有人能帮忙吗?谢谢你。以下是我使用的代码。

  add_action( 'publish_post', 'save_in_all_sites'  );

  function save_in_all_sites( $post_id ){

    global $sitepress;

         $my_post = array(
         'post_title'    => $post_title,
         'post_content'  => $post_content,
         'post_status'   => $post_status

         );

        $def_trid = $sitepress->get_element_trid($post_id);

        $ru_post_id1 = wp_insert_post( $my_post );
        // insert the post in German language 

          $sitepress->set_element_language_details($ru_post_id1, 'post_post', $def_trid, 'de');

   }

英语代码在哪里?是 $def_trid 吗?如果是,那么您将英语和德语语言都设置为 element_language_details 所以当您得到它时,它 returns 您所有的英语和德语语言都尝试在您的数据库中使用单独的列分别设置它们.

应该可以在您 wp_insert_post 之前删除挂钩,然后在之后立即将其添加回去。

示例

remove_action( 'publish_post', 'save_in_all_sites'  );
$ru_post_id1 = wp_insert_post( $my_post );
add_action( 'publish_post', 'save_in_all_sites'  );