Php Wordpress-通过元信息更新帖子

Php Wordpress-通过元信息更新帖子,php,database,wordpress,post,Php,Database,Wordpress,Post,我正在寻找一个解决方案,我可以通过元信息而不是id来更新帖子 我通过jsonp从外部资源获得了我的内容。我将内容保存到数据库中 我找到的唯一方法是,通过id更新帖子——但我没有id。我拥有的是来自其他资源的文章id。我将这篇文章id作为元信息保存到每个wp帖子中 那么,有没有办法通过帖子的元信息来更新帖子?好的, 下面是一些代码和我现在所做的工作: public function saveArticleInWpDatabase($json_data){ for ($i = 0; $i &

我正在寻找一个解决方案,我可以通过元信息而不是id来更新帖子

我通过jsonp从外部资源获得了我的内容。我将内容保存到数据库中

我找到的唯一方法是,通过id更新帖子——但我没有id。我拥有的是来自其他资源的文章id。我将这篇文章id作为元信息保存到每个wp帖子中

那么,有没有办法通过帖子的元信息来更新帖子?

好的, 下面是一些代码和我现在所做的工作:

public function saveArticleInWpDatabase($json_data){
    for ($i = 0; $i < count($json_data); $i++) {

        $articleId = $json_data[$i]['articleId'];
        $img = $json_data[$i]['img'];

        $articleHeadline = $json_data[$i]['articleContent']['headline'];
        $articleContent = $json_data[$i]['articleContent']['content'];

        $termsInput = $json_data[$i]['articleContent']['terms'];
        $terms = explode(',',$termsInput);
        $creationDate = $json_data[$i]['articleCreationDate'];
        $modifiedDate = $json_data[$i]['articleModifiedDate'];

    //Check if post with article ID in postmeta exists
        $existingId = $this->wpdb->get_var("SELECT post_id FROM ".$this->wpdb->postmeta." WHERE (meta_key = 'article_id' AND meta_value = '".$articleId."')");

        //Posts doesn't exist
        if ($existingId == null) {

            $my_post = array(
                'post_date' => $creationDate,
                'post_modified' => $modifiedDate,
                'post_title' => $articleHeadline,
                'post_content' => $articleContent,
                'post_status' => 'publish',
                'post_author' => 1,
                'post_category' => array(0, $categoryId)
            );

            $last_id = wp_insert_post($my_post);

            add_post_meta($last_id, 'article_id', $articleId, true);
            wp_set_post_categories($last_id, array(0,$categoryId), true);
            wp_set_post_terms( $last_id,$terms, 'post_tag', true );

            //$this->_import_photo($last_id, $img);
        }
        //Post exists
        else{
            $my_post = array(
                'ID' => $existingId,
                'post_date' => $creationDate,
                'post_modified' => $modifiedDate,
                'post_title' => $articleHeadline,
                'post_content' => $articleContent,
                'post_status' => 'publish',
                'post_author' => 1,
                'post_category' => array(0, $categoryId)
            );

            //Get last modified date and compare it with the new one
            $lastUpdate = $this->wpdb->get_var("SELECT post_modified FROM " . $this->wpdb->posts . " WHERE ID = ".$existingId);

            if(strcmp($lastUpdate, $modifiedDate) != 0){
                var_dump("Update article with id: " . $existingId);
                $last_id = wp_update_post($my_post);
            }
            else{
                var_dump("Article: " . $existingId . " Nothing changed");
            }

        }
    }
}
但它只在两列中保存articleCreationDate(post_日期,post_修改)

那么,有没有人有更好的解决方案或想法?有人知道吗,为什么它不从我的json中保存修改过的日期值

干杯

欢迎来到SO。请阅读并发布相关代码,以便人们能够帮助您。
"articleCreationDate": "2012-05-21 14:38:29", "articleModifiedDate": "2016-02-11 14:52:01"