php - 尝试从数据库表行在 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()
解决方案
已经修好了,谢谢!将它包装在一个函数中并执行它,这就解决了所有问题。
推荐阅读
- r - 什么是用于 k-means 初始化的 NbClust 方法?
- mysql - 错误 2002 (HY000): 无法通过 ubuntu 服务器中的套接字“/var/run/mysqld/mysqld.sock”连接到本地 MySQL 服务器
- swift - Swift 中的计算属性和存储值属性有什么区别?
- laravel - Laravel 自定义密码邮箱
- sql - 查询内的多个替换
- python - 使用 Flask 从提交的 HTML 表单值重定向链接
- android - appcompat-v7:28.0.0-rc02 不适用于 design:28.0.0-rc01(应该是 rc02,但我们不能)
- android - 从 PHP 服务器获取 JSON 数据
- ignite - 阿帕奇点燃 | TCP 发现 SPI
- python - 使用 PYTHON 和 JSON 传递 API 页面