2017-03-03 84 views
0

我需要更改插件,创建/编辑条目到wp_posts表。目前缺乏它不能针对某些特定领域的问题。我正在努力编辑插件,以便我可以使用插件更改这些字段。下面的细节。如何在WordPress中从插件php编辑mySQL wp_posts表?

我通常会如何创建/编辑条目?

我心里有这样的事情,这是从here复制:

$my_post = array(
    'post_title' => '#Enter the title#', 
    'post_content' => '#Enter the Content#', 
    'post_status' => 'publish', 
    'post_author' => 1 
); 

wp_insert_post($my_post); 

我想这是可能的,因为我希望改变已经插件使更改wp_posts表。我该怎么做呢?我在C#中有编程经验,但我的PHP和WP知识严重缺乏。我假设我正在寻找一些WordPress key functions,但我根本不知道它们是什么或如何找到它们(我可能会搜索错误的术语)。 wp_insert_post()看起来很有希望,但the documentation似乎暗示它不适合我的特定需求。


具体细节我使用了一个名为Really Simple CSV importer(RSC)的优秀插件,它允许您使用CSV创建/更新帖子

。它似乎能够瞄准几乎每个领域(例如,post_title,post_id,post_parent,post_content,_wp_page_template等)。如果你必须定期创建数百页(这是我的),这是非常有益的。

问题是它不会针对标题为group_accesstag_list的两个特定字段。我把这些字段作为列放在我的csv中,但它们没有添加到mySQL数据库中,因此在网站上没有更改。我相信这些字段特定于名为iMember360的插件,该插件是一个与InfusionSoft帐户关联的插件。

当然,我已经尝试联系RSC支持,但没有收到任何回复。我已经和iMember360支持进行了长时间的对话,并且他们可以给我的最好的结果是他们使用动作钩import_end来更改表格,所以如果RSC没有使用它,那么它不会影响这些领域。他们还说iMember360具有导入/导出功能,但它仅用于插件的特定功能,并且与WordPress XML导入/导出功能相关。显然,这个RSC插件不会那样做。

无论这些限制如何,在我看来,如果该字段存在于表格中,那么您应该可以对其进行编辑,所以我倾向于认为RSC插件完全缺乏此功能,可能只是定位只有WP默认字段。


我已经试过什么:

我曾尝试直接编辑插件PHP,并假设该插件根本没有对group_accesstag_list领域的条目。

一个PHP文件包含:

// (string, comma separated) name of post tags 
$post_tags = $h->get_data($this,$data,'post_tags'); 
if ($post_tags) { 
    $post['post_tags'] = $post_tags; 
} 

我只是复制并粘贴它正上方,改变post_tagsgroup_access。它没有工作。

我还发现,并添加到:

$attachment = array(
      'post_mime_type' => $mime_type , 
      'post_parent' => $this->postid , 
      'post_author' => $this->post->post_author , 
      'post_title'  => $title , 
      'post_content' => $content , 
      'post_excerpt' => $excerpt , 
      'post_status' => 'inherit', 
      'menu_order'  => $this->media_count + 1, 
////////// added 
      'group_access' => $group_access , 
      'tag_list' => $tag_list , 
////// 
     ); 

这并没有做任何事情无论是。

回答

0

有两种方法可以编辑我知道的wp_posts表。首先是wp_update_post()wp_insert_post()函数。这些是典型的WP功能,与其他任何其他功能一样。你只需要传递正确的参数。例如:

$my_post = array(
     'ID'   => $updatePostID, // User defined variable; the post id you want to update. 
     'post_title' => 'Update Title', 
     'post_content' => 'Update Content', 
); 
// Update the post into the database 
$post_id = wp_update_post($my_post, true); // Returns 0 if no error; returns post id if there was an error. 

// Check if table was updated without error. For debugging. Remove from your code if it all works, before publishing. 
if (is_wp_error($post_id)) { 
    $errors = $post_id->get_error_messages(); 
    foreach ($errors as $error) { 
     echo $error; 
    } 
} 

只需将具有预定义字段名称的数组传递给函数,然后它们将正确运行。更新函数自然是用于更新,插入函数创建新的表项(行)。但是,有一个限制。这两个函数只能在WP预定义的字段上运行。所以,如果你的表格包含一些自定义字段,那么你将不得不使用$wpdb类。

另一种从WordPress更新mySQL表格的方法是直接使用$wpdb类。这个类比wp_update_post()wp_insert_post()函数要广泛得多。第一个区别是它可以编辑许多其他mySQL表格,而不仅仅是wp_posts表格。它也可以编辑非WP字段。例如:

global $wpdb; // This calls the use of the class. I actually do not think this is necessary, but I do it anyway. 
$dbresult = $wpdb->update($wpdb->posts, // Returns 0 if no errors. Post ID if errors occurred. 
      // Notice posts instead of wp-posts. Prefixes are not necessary with this class. 
    ['post_title' => 'Update Title', 
     'post_content' => 'Update Content', 
     'custom_field' => $customField, ], // User defined table column and variable. 
    ['ID' => $updatePostID, ]); // User defined variable; the post id. 
if (false === $dbresult) { 
    echo 'An error occurred while updating...'; 
    $errors = $post_id->get_error_messages(); 
} 

我不是很熟悉这些选项之一,但似乎在功能宽度的巨大的差异,所以有可能是你使用一个比其他前应采取的注意事项。我问了一个问题,为此:https://wordpress.stackexchange.com/q/259043/38365一个reply说:

$wpdb

缺点是,你需要了解SQL,你需要意识到正常的数据消毒的以及何时使用prepare()什么功能已经准备好您的声明,这几乎是不利的。 $wpdb类本质上比任何默认的WordPress功能更强大,因为有权访问所有数据,自定义与否,假设你知道正确的SQL来获取,修改或删除它。

TL; DR$wpdb更强大但不友善和宽容。

这对我有意义。基本上,不要拨弄$ wpdb,除非你知道你在做什么或绝对必须做什么。