【问题标题】:Wordpress - hooking into saving post dataWordpress - 连接到保存帖子数据
【发布时间】:2013-04-03 16:30:01
【问题描述】:

如果 post 自定义字段设置为 true,则这段代码应该设置 post 数据,如果不是,则将 post status 设置为草稿。该动作需要在post_save/post_updated 动作之后调用,所以我们的一所大学向我推荐了方法(老问题:wordpress set post_status as "draft" in 'save_post' action):

add_filter( 'wp_insert_post_data', 'set_post_parameters', 99, 2 );
function set_post_parameters( $data, $postarr ) {
    if ( get_post_meta( $postarr['ID'], '_cs_tweet_ok_status', true ) == 'true' ) {
        $data['post_date']      = get_post_meta( $postarr['ID'], '_cs_system_tweet_date', true );   
        $data['post_date_gmt']  = get_gmt_from_date( get_post_meta( $postarr['ID'], '_cs_system_tweet_date', true ) );
    } else {
        $data['post_status'] = 'draft';
    }
    return $data;
}

它会做它应该做的 - 它将发布日期设置为提供自定义字段(之前保存)的日期或将帖子设置为草稿。 唯一的事情是,当post-new.php 被调用时,它也会被触发,那些创建无法删除的“空”帖子(以防用户没有填写所有数据并且没有正确保存帖子)。 我应该如何在该函数中创建一个“检查”,以便它仅在保存或更新帖子时运行?

我在设置post_data 的功能时也有一个小问题:

add_action( 'save_post', 'cs_twitter_save_meta' );
function cs_twitter_save_meta( $post_id ) {
    if ( isset($_POST['post_type']) && 'tweets' == $_POST['post_type'] ) :
        if ( isset($_POST['post_title']) ) :
            $url = 'https://api.twitter.com/1/statuses/show.json?id='.$_POST['post_title'];
            $json = @file_get_contents($url,0,null,null);
            if ( $json != false ) {
                $json_output = json_decode($json);
                $post_date = date('c', strtotime($json_output->created_at));
                $post_save_date = date('G:i - d M y', strtotime($json_output->created_at));
                update_post_meta( $post_id, '_cs_tweet_content', $json_output->text  );
                update_post_meta( $post_id, '_cs_tweet_date', $post_save_date  );
                update_post_meta( $post_id, '_cs_system_tweet_date', $post_date  );
                update_post_meta( $post_id, '_cs_tweet_user', $json_output->user->screen_name );
                update_post_meta( $post_id, '_cs_tweet_ok_status', 'true' );
            } else {
                update_post_meta( $post_id, '_cs_tweet_content', 'There is an error with tweet Api. Too many connections in one hour. Try again after 60 minutes' );
                update_post_meta( $post_id, '_cs_tweet_ok_status', 'false'  );
            }
        endif;
    endif;
}

第一次保存时出现问题。似乎 add_filter( 'wp_insert_post_data', 'set_post_parameters', 99, 2 ); 首先触发,没有看到 custom_meta 并将 post_status 设置为草稿。然后add_action( 'save_post', 'cs_twitter_save_meta' ); 触发,但第一个脚本已经太晚了。

所以另一个问题 - 我应该如何解决这种情况,以便首先正确设置所有数据: 1. 用户创建新的 custom_post_type。 2. 输入推文id 导入,推送发布。 3. 脚本检查它是否可以从 twitter api 获取数据 4. 如果可以,它会获取所有必要的数据,发布帖子并将其发布日期设置为推文的发布日期。 5. 如果没有,它会显示有关错误的信息并将post_status 设置为草稿。

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    这应该可以解决您的问题。而不是使用wp_insert_post_data 挂钩,只使用save_post 并使用$wpdb 来更改数据库条目。

    add_action( 'save_post', 'cs_twitter_save_meta' );
    function cs_twitter_save_meta( $post_id ) {
        if ( isset($_POST['post_type']) && 'tweets' == $_POST['post_type'] ) :
            if ( isset($_POST['post_title']) ) :
                $url = 'https://api.twitter.com/1/statuses/show.json?id='.$_POST['post_title'];
                $json = @file_get_contents($url,0,null,null);
                if ( $json != false ) {
                    $json_output = json_decode($json);
                    $post_date = date('Y-m-d H:i:s', strtotime($json_output->created_at));
                    $post_date_gmt = get_gmt_from_date( $post_date );
                    $post_save_date = date('G:i - d M y', strtotime($json_output->created_at));
                    update_post_meta( $post_id, '_cs_tweet_content', $json_output->text  );
                    update_post_meta( $post_id, '_cs_tweet_date', $post_save_date  );
                    update_post_meta( $post_id, '_cs_system_tweet_date', $post_date  );
                    update_post_meta( $post_id, '_cs_tweet_user', $json_output->user->screen_name );
                    update_post_meta( $post_id, '_cs_tweet_ok_status', 'true' );
                    global $wpdb;
                    $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_date = '$post_date', post_date_gmt = '$post_date_gmt' WHERE id = $post_id", $post_id ));
                } else {
                    update_post_meta( $post_id, '_cs_tweet_content', 'There is an error with tweet Api. Too many connections in one hour. Try again after 60 minutes' );
                    update_post_meta( $post_id, '_cs_tweet_ok_status', 'false'  );
                    global $wpdb;
                    $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_status = 'draft' WHERE id = $post_id", $post_id ));
                }
            endif;
        endif;
    }
    

    【讨论】:

      【解决方案2】:

      您可以尝试阻止该函数运行,除非您明确点击保存。

      在函数的开头添加以下 sn-p:

      // Autosave
      if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
              return;
      // AJAX
      if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) 
              return;
      // Post revision
      if ( false !== wp_is_post_revision( $post_id ) )
              return;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-16
        • 1970-01-01
        • 2013-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多