【问题标题】:I want to update My Wordpress Post right after publishing the Post?我想在发布帖子后立即更新我的 Wordpress 帖子?
【发布时间】:2022-08-14 07:10:59
【问题描述】:

我想在发布帖子后立即更新我的 WordPress 帖子。下面给出的代码更新了帖子,但我的仪表板一直加载,直到出现超时错误。创建帖子后,我想添加更多内容。

 function updatemypost( $post_id, $post,$update ) {
        
        $mydata = array(
      \'ID\' => $post_id,
      \'post_content\' => \'New Data..\',
        );
     
    wp_update_post( $mydata );
       
    }
    add_action( \'publish_post\', \'updatemypost\', 10, 3 );

    标签: wordpress hook


    【解决方案1】:

    publish_post 会在您每次更新帖子时触发,不仅在首次发布时触发,因此您最终会得到一个 inifine 循环。

    您可以通过在触发 update_post 之前删除操作来修复它,就像这样

    function updatemypost( $post_id, $post,$update ) {
            
        $mydata = array(
          'ID' => $post_id,
          'post_content' => 'New Data..',
        );
        
        remove_action( 'publish_post', 'updatemypost', 10 );
        wp_update_post( $mydata );
        add_action( 'publish_post', 'updatemypost', 10, 3 );
           
    }
    add_action( 'publish_post', 'updatemypost', 10, 3 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-06
      • 1970-01-01
      • 2013-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 1970-01-01
      相关资源
      最近更新 更多