我需要使用wp_insert_post创建wordpress帖子,并填写" post_title"和" post_content"从外部(非wordpress)数据库查询值。无论我尝试什么,我仍然会得到同样的错误。
Wordpress Database Error: database_name.wp_phppc_functions' doesn't exist]
SELECT * FROM `wp_phppc_functions` WHERE `id` = 31
我已尝试将以下代码作为插件,插入到functions.php中并作为独立文件但我得到了同样的错误。出于某种原因,该网站仍然认为它在其他数据库中?你能给我的任何帮助都会非常感激!
$conzz = mysql_connect("localhost","username","password");
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");
while($row = mysql_fetch_array($resultzz)) {
$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)
);
$conzzz = mysql_connect("localhost","username","password","wordpress_database");
if (!$conzzz) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("wordpress_database", $conzzz);
wp_insert_post( $new_post );
}
?>
答案 0 :(得分:1)
您获得的错误与您的插件无关,它是PHP Code for Posts插件缺少一个表 - 您应该禁用或删除此插件。请参阅此文件中包含表名的代码:http://plugins.svn.wordpress.org/php-code-for-posts/tags/1.2.0/PHPPostCode.php
也就是说,你的插件也存在问题。您不应该使用mysql_*
扩展名,因为它们已被弃用,并且因为它们不是使用WordPress访问数据库的正确方法。执行此操作的正确方法是使用全局变量$wpdb
,它将大量数据库工作从您的插件中抽象出来。使用推荐的WordPress函数实现代码的更好方法......
global $wpdb;
$sql = <<<SQL
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
SQL;
$rebates = $wpdb->get_results( $sql );
foreach ( $rebates as $rebate ){
$new_post = array(
'post_title' => $rebate->REBATE_CODE,
'post_content' => $rebate->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)
);
if ( 0 == ( $post_id = wp_insert_post( $new_post ) ) ){
// an error occurred and $post_id == 0
} else {
// the new post_id is $post_id
}
}