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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

That 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