douleng3463 2018-10-23 16:22
浏览 53

尝试从数据库表行创建Wordpress中的帖子

I'm attempting to write a plugin to automatically create posts based on rows in a new database table, however I'm having issues as it's looping, and if I put an echo, it would echo out code, but it's not creating the new posts. I'm not getting any error messages either. What am I missing?

EDIT: I have modified my code

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 );
};

But now it's only doing the first row, and returning an error:

Fatal error: Uncaught Error: Call to undefined function is_user_logged_in() in /app/public/wp-includes/post.php:2283

  • 写回答

1条回答 默认 最新

  • doutangu4671 2018-10-23 18:35
    关注

    Got it fixed, thanks! wrapped it in a function and executed it, and that fixed all the issues.

    评论

报告相同问题?

悬赏问题

  • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
  • ¥15 cs loadimage运行不了,easyx也下了,没有用
  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥20 关于变压器的具体案例分析