【问题标题】:Wordpress and wp_insert_post not workingWordpress 和 wp_insert_post 不工作
【发布时间】:2013-11-13 17:28:55
【问题描述】:

我正在做一个适用于 wordpress 网站的 webapp。

直到昨天,一切都像魅力一样,但是从今天开始,当我将帖子保存在 wordpress db 上时,它现在无法正常工作... 不给我错误(php错误或wordpress错误),而只是一个白页。

这是我的完整代码:(很抱歉有许多注释行和回显,但我正在测试)

重要的部分:

  // Create post object
  $my_post = array(
    'post_title'    => $article_title,
    'post_name'   => $article_title,
    'post_content'  => $article_content,
    'post_status'   => 'pending',
    'post_author'   => 1
  );

  // Insert the post into the database
  wp_insert_post($my_post, TRUE);// try first time

  $post_status = "pending";
  $post_type = "post";
  $post_parent = "0";

  $post_ID = (int) $wpdb->insert_id;
  echo $post_ID; die;

昨天用一个简单的 wp_insert_post(my_post);今天一切正常(当然我认为我编辑了一些文件......或者我现在没有),不能工作更多并且不会给我错误。

你怎么看?

谢谢你,真的!

编辑:问题是我没有插入 post_category 并且 wp 需要它!

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您可以使用 wp_insert_post() 函数捕获 $post_id...

    $post_ID = wp_insert_post($my_post, TRUE);// try first time
    

    然后您可以转储出 $post_ID 变量以获取 ID 或错误。

    var_dump($post_ID);
    

    【讨论】:

      【解决方案2】:
      add_action( 'init', 'my_func' );
      
      function my_func()  {
      $my_post = '';
      if( get_page_by_title($article_title,'OBJECT','post') == NULL )
      $my_post= array(
          'post_title'    => $article_title,
          'post_name'   => $article_title,
          'post_type'  => 'post',
          'post_content'  => $article_content,
          'post_status'   => 'pending',
          'post_author'   => 1
      );
      $post_id = wp_insert_post( $my_post );
      echo 'Post ID - '.$post_id;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-03-29
        • 2022-06-10
        • 1970-01-01
        • 2013-08-12
        • 2015-10-11
        • 2014-06-30
        • 1970-01-01
        相关资源
        最近更新 更多