admin管理员组文章数量:1389810
I'm trying to code a loop that will post articles from a SQL database. However, whenever I try to load the page I get a 500 error and nothing is showing up in the error logs.
Code:
<?php
define('WP_USE_THEMES', false);
require('wp-load.php');
global $wpdb;
$sql = "SELECT * FROM newposts";
$result = $wpdb->query($sql);
while($row = $result->fetch_assoc()) {
$post = array(
'post_title' => $row['post_title'],
'post_content' => $row['post_content'],
'post_date' => $row['post_date'],
'post_status' => 'publish',
'post_author' => 1,
'post_category' => 1
);
wp_insert_post( $post );
echo "inserted post {$row['post_title']}";
echo "<br />";
}
?>
I'm pretty new to wordpress so I don't really know if I'm using $wpdb or the insert post function correctly. Also, the file is placed in the same directory as wp-load, wp-config, etc. And the site is running on bluehost, if that means anything (I don't know).
Thanks for your time!
I'm trying to code a loop that will post articles from a SQL database. However, whenever I try to load the page I get a 500 error and nothing is showing up in the error logs.
Code:
<?php
define('WP_USE_THEMES', false);
require('wp-load.php');
global $wpdb;
$sql = "SELECT * FROM newposts";
$result = $wpdb->query($sql);
while($row = $result->fetch_assoc()) {
$post = array(
'post_title' => $row['post_title'],
'post_content' => $row['post_content'],
'post_date' => $row['post_date'],
'post_status' => 'publish',
'post_author' => 1,
'post_category' => 1
);
wp_insert_post( $post );
echo "inserted post {$row['post_title']}";
echo "<br />";
}
?>
I'm pretty new to wordpress so I don't really know if I'm using $wpdb or the insert post function correctly. Also, the file is placed in the same directory as wp-load, wp-config, etc. And the site is running on bluehost, if that means anything (I don't know).
Thanks for your time!
Share Improve this question asked Jul 9, 2016 at 0:40 angelaangela 133 bronze badges1 Answer
Reset to default 1That is because you're trying to define a property that is already defined, query a table with a wrong name, loop through an integer while you can use get_results
instead of query
method of $wpdb
...
I edited the code, hope it will help with your post duplication process:
defined( 'WP_USE_THEMES' ) || define( 'WP_USE_THEMES', false );
require_once('wp-load.php');
global $wpdb;
$sql = "SELECT * FROM {$wpdb->prefix}newposts";
$result = $wpdb->get_results( $sql );
foreach ( $result as $row ):
$row = ( array ) $row;
$post = array(
'post_title' => $row['post_title'],
'post_content' => $row['post_content'],
'post_date' => $row['post_date'],
'post_date_gmt' => $row['post_date_gmt'],
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(1)
);
wp_insert_post( $post );
echo "inserted post {$row['post_title']}";
echo "<br />";
endforeach;
本文标签: phpCan39t get wpinsertpost to work
版权声明:本文标题:php - Can't get wp_insert_post to work 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744708995a2621013.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论