【问题标题】:Create multiple WordPress posts using wp_insert_post without loop使用没有循环的 wp_insert_post 创建多个 WordPress 帖子
【发布时间】:2021-04-08 10:04:42
【问题描述】:

我必须插入多个帖子。一个一个地插入帖子需要时间。他们有什么方法可以在一个查询中插入所有帖子($array)吗?

$my_post = array(
                  'post_title'    => $row->title,
                  'post_content'  => $row->content,
                  'post_status'   => 'publish',
                  'post_date_gmt'   => $row->created_at,
                  'post_date'   => $row->created_at,
                  'post_author'   => 1,
                  'post_excerpt'   => 'test',
                  'tags_input'   => $tags,
                  'post_category' => array(1,2)
                );
    wp_insert_post( $my_post );

}

【问题讨论】:

    标签: wordpress posts


    【解决方案1】:

    其实你已经很接近了! wp_insert_post 是正确的方法,但我们需要先检查帖子/页面是否真的存在。我们可以使用get_page_by_title 做到这一点。然后我们只需要为我们的帖子/页面的标题和内容创建一个数组,并使用带有键的foreach 循环遍历它。

    <?php
    add_action( 'init', 'theme_pages' );
    function theme_pages() {
      $pages = array(
        'Contact' => 'This is a contact page.',
        'Press' => 'This is a press page.',
        'Help' => 'This is an help page.',
      );
      foreach ( $pages as $title => $content ) {
        $type = 'page';
    
        /**
        * get_page_by_title
        * Retrieve a page given its title.
        * @link https://developer.wordpress.org/reference/functions/get_page_by_title/
        */
        if ( ! get_page_by_title( $title, OBJECT, $type ) ) {
    
          /**
          * wp_insert_post
          * Insert or update a post.
          * @link https://developer.wordpress.org/reference/functions/wp_insert_post/
          */
          wp_insert_post( array(
            'post_title' => $title,
            'post_content' => $content,
            'post_status' => 'publish',
            'post_type' => $type,
          ) );
        };
      };
    }; ?>
    

    了解详情

    【讨论】:

      【解决方案2】:

      如果您需要 WordPress 来完成与创建帖子相关的所有工作,那么像 wordpress.stackexchange.com 上的this answer 中提到的那样,一次性插入多个帖子并不容易。这是因为wp_insert_post 做了很多事情,比如将数据添加到wp_postswp_postmeta 表中。有关详细信息,请参阅source code 了解该功能。

      但是,从技术上讲,您可以执行直接 SQL 查询,但它不会添加帖子元数据、清理数据或运行任何插件或主题过滤器。这是一个模板查询,您可以为要创建的每个帖子填写它,然后使用mysqli_query 或类似的方式对数据库运行它:

      INSERT INTO `wp_posts` (`ID`, `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_excerpt`, `post_status`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`, `menu_order`, `post_type`, `post_mime_type`, `comment_count`) VALUES (NULL, '0', '0000-00-00 00:00:00.000000', '0000-00-00 00:00:00.000000', '', '', '', 'publish', 'open', 'open', '', '', '', '', '0000-00-00 00:00:00.000000', '0000-00-00 00:00:00.000000', '', '0', '', '0', 'post', '', '0');
      

      但是,我再次建议不要这样做。一次一个有点慢,但你会得到很多额外的功能作为回报,除非你添加非常非常多的帖子,否则性能应该不会那么糟糕。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-25
        • 1970-01-01
        • 2015-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-30
        相关资源
        最近更新 更多