【问题标题】:How to populate Wordpress Posts using wp_insert_post and SQL query如何使用 wp_insert_post 和 SQL 查询填充 Wordpress 帖子
【发布时间】:2014-06-30 06:28:25
【问题描述】:

我正在从事一个项目,我想使用来自外部数据库的 SQL 查询以编程方式创建 wordpress 帖子。我已经能够填充 HighCharts 图表并以表格格式显示数据,使用 PHP 查询 SQL 数据库。但是,我不知道如何以同样的方式使用 wp_insert_post。我也不确定我应该把这段代码放在哪里。任何帮助深表感谢!这是我的 PHP。

<?php
/**
 * Plugin Name: Post Creator

 * Description: Matt's Post Creator
 * Version: 0.2
 * Author: Matt 
 * Author URI:
 * License: GPLv2
 */

/**
 * Enable PHP in Widgets
 */

$conzz = mysql_connect("localhost","database","databasepassword");





if (!$conzz) {


  die('Could not connect: ' . mysql_error());


}





mysql_select_db("chiensi_testing", $conzz);




$resultzz = mysql_query("SELECT T1.ID, REBATE_CODE, LONG_DESC, INCENT_TECH_ID, UPGRADE_TECH FROM T_L_INCENTIVES T1 INNER JOIN T_L_INCENT_TECH T2 ON T1.L_INCENT_TECH = T2.ID INNER JOIN T_LIGHTING_TYPE T3 ON T2.E_LIGHTING_TYPE_ID = T3.ID");



while($row = mysql_fetch_array($resultzz)) {


  global $user_ID;
$new_post = array(
'post_title' =>  $row['REBATE_CODE'] ,
'post_content' =>  $row['LONG_DESC'] ,
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'post',
'post_category' => array(10)
);
$posts_array[]=$new_post;

}

mysqli_close($conzz);

foreach ( $posts_array as $post ) {

$post_id = wp_insert_post($post);
};


?>

【问题讨论】:

    标签: php mysql sql wordpress


    【解决方案1】:

    在以下评论后编辑:

    在尝试调用 wp_insert_post 之前尝试终止与第一个数据库的连接

    $conzz = mysql_connect("localhost","database","databasepassword");
     if (!$conzz) {
    die('Could not connect: ' . mysql_error());
    
    
    }
    mysql_select_db("database_name", $conzz);
    
     $resultzz = mysql_query("SELECT T1.ID, REBATE_CODE, LONG_DESC, INCENT_TECH_ID, UPGRADE_TECH FROM T_L_INCENTIVES T1 INNER JOIN T_L_INCENT_TECH T2 ON T1.L_INCENT_TECH = T2.ID INNER JOIN T_LIGHTING_TYPE T3 ON T2.E_LIGHTING_TYPE_ID = T3.ID");
    
    $posts_array = array();
    
    while($row = mysql_fetch_array($resultzz)) {
    
    
      global $user_ID;
    $new_post = array(
    'post_title' =>  $row['REBATE_CODE'] ,
    'post_content' =>  $row['LONG_DESC'] ,
    'post_status' => 'publish',
    'post_date' => date('Y-m-d H:i:s'),
    'post_author' => $user_ID,
    'post_type' => 'post',
    'post_category' => array(10)
    );
    $posts_array[]=$new_post;
    
    }
    
    mysqli_close($conzz);
    
    foreach ( $posts_array as $post ) {
    
    $post_id = wp_insert_post($post);
    };
    
    
    
    ?>
    

    【讨论】:

    • 您好,感谢您的帮助!我设置与数据库的连接的原因是因为它是一个独立于 Wordpress 数据库的外部数据库。我需要从这个单独的数据库中提取值并使用结果创建 WP 帖子。
    • 然后你需要使用你展示的sql连接,将返回值存储为一个数组,然后使用这些返回值作为参数调用wp_insert_post
    • @Matt Shirk 您是否从代码中收到任何错误?
    • 嗨 - 我现在没有收到任何错误,但也没有创建任何帖子。我尝试将我的代码添加为插件,但是当我尝试“激活”它时,什么也没有发生。我尝试将我的代码添加到“functions.php”,但随后我被踢出管理仪表板并且无法重新登录,直到我手动从“functions.php”中删除了代码。我认为问题可能在于将数组项添加到 wp_insert_post 函数。我这样做正确吗?再次感谢您的帮助!
    • 你能检查一下你在while循环中构建的数组吗?而不是调用 wp_insert_post var_dump( $new_post); 另外,您不应该因为将其添加到您的functions.php而被锁定在管理员之外,无论导致什么都可能是您所有麻烦的根源。是死亡的白屏吗?
    猜你喜欢
    • 2015-11-04
    • 2022-06-10
    • 2018-08-13
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 2021-04-08
    相关资源
    最近更新 更多