首页 > 解决方案 > 尝试从数据库表行在 Wordpress 中创建帖子

问题描述

我正在尝试编写一个插件来根据新数据库表中的行自动创建帖子,但是我在循环时遇到了问题,如果我放一个回显,它会回显代码,但它不会创建新的帖子。我也没有收到任何错误消息。我错过了什么?

编辑:我修改了我的代码

global $wpdb;
$rows = $wpdb->get_results("SELECT * FROM routes_txt");
foreach ( $rows as $row ) {

    // Insert the post into the database
    $my_post = array(
      'post_title'    => $row->route_short_name,
      'post_content'  => $row->route_id,
      'post_status'   => 'publish',
      'post_type'     => 'routes',
      'post_author'   => get_current_user_id(),
    );

    wp_insert_post( $my_post );
};

但现在它只做第一行,并返回一个错误:

致命错误:未捕获的错误:调用 /app/public/wp-includes/post.php:2283 中未定义的函数 is_user_logged_in()

标签: phpsqlwordpressplugins

解决方案


已经修好了,谢谢!将它包装在一个函数中并执行它,这就解决了所有问题。


推荐阅读