1

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!

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;
| improve this answer | |
  • Thank you. I don't know about the first point, and the table definitely had a correct name (I created the table manually myself, so there is no prefix), but the rewritten code is no longer fetching a 500 error, and the echo is working so I know it's fetching the data properly. However, it doesn't seem like the function is actually insterting the posts. Any idea why? – angela Jul 9 '16 at 1:59
  • Actually, it appears they are inserting, but only if I leave the "post_date" field blank. If you don't know a way around this, it's no big deal, but it would be nice to preserve dates when copying these posts over. – angela Jul 9 '16 at 2:04
  • Yes @angela I think it's because of the missing key post_date_gmt let me edit the answer.. – Samuel Elh Jul 9 '16 at 2:24
  • 1
    I figured out it's because the dates I were trying to import were in the wrong format. I just changed them by adding this line $wpdate = date("Y-m-d H:i:s", strtotime($row['post_date'])); and it worked! Thank you for all of the help :) – angela Jul 9 '16 at 4:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.