【问题标题】:How to edit mySQL wp_posts table from plugin php in WordPress?如何从 WordPress 中的插件 php 编辑 mySQL wp_posts 表?
【发布时间】:2017-07-24 00:09:06
【问题描述】:

我需要更改创建/编辑 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_titlepost_idpost_parentpost_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_tags 更改为group_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 ,
//////
        );

那也没有用。

【问题讨论】:

    标签: php mysql wordpress plugins


    【解决方案1】:

    据我所知,有两种方法可以编辑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;
        }
    }
    

    只需将具有预定义字段名称的数组传递给函数,它们就会正确运行。 update 函数自然用于更新,而 insert 函数创建新的表条目(行)。但是,有一个限制。这两个函数只会对 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。

    【讨论】:

      猜你喜欢
      • 2016-07-29
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 2019-11-12
      • 1970-01-01
      • 2023-02-24
      • 2015-02-13
      • 1970-01-01
      相关资源
      最近更新 更多