WordPress.org

Ready to get started?Download WordPress

Forums

$wpdb->posts fails to insert (2 posts)

  1. areavisbey
    Member
    Posted 5 years ago #

    I am trying to insert posts into the database programmatically using the following line. when the line is hit the code breaks.

    $wpdb->query("INSERT INTO $wpdb->posts (post_author, post_date, post_date_gmt, post_content, post_title, post_name, post_modified, post_modified_gmt, comment_count) VALUES ($userid, '$now', '$now_gmt', '".$post_content."', '".$post_title."', '".$post_name."', '$now', '$now_gmt', '1')");

    i've tried outputting the line with:

    logDDFM("INSERT INTO $wpdb->posts (post_author, post_date, post_date_gmt, post_content, post_title, post_name, post_modified, post_modified_gmt, comment_count) VALUES ($post_author, '$now', '$now_gmt', '".$post_content."', '".$post_title."', '".$post_name."', '$now', '$now_gmt', '0')");

    Note: logDDFM is logging method i made to write to a file. The output is as follows:
    INSERT INTO (post_author, post_date, post_date_gmt, post_content, post_title, post_name, post_modified, post_modified_gmt, comment_count) VALUES (2, '2009-01-05 14:59:03', '2009-01-05 21:59:03', 'wowwser', 'This-works-now', 'This works now', '2009-01-05 14:59:03', '2009-01-05 21:59:03', '0')

    As you can see the table name is not in the statement. Why is that? and how can i get this post inserted manually.

    Also, is there an error log for WordPress. I'm using 2.5 version. Where can i find it if so.
    Thanks!

  2. Austin Matzko
    Member
    Posted 5 years ago #

    Have you declared $wpdb as a global variable? If not, or you're not using it within the scope of WordPress's global variables, then that would explain why wp_posts is missing.

    If you are creating these queries within WordPress, then you are probably better off not making MySQL queries directly but instead using WP's post API, such as wp_insert_post().

Topic Closed

This topic has been closed to new replies.

About this Topic