EDIT: Turns out I had a permalink issue that was causing the 404 error.

Original Posting below:

I am creating a page programmatically using $wpdb->insert() and the resulting page returns a 404 error.

A few things to note

  • I am using a $wpdb object to create the page because I am creating it on a separate WordPress website, so wp_insert_post() doesn't work, because it would insert the post into the website I making the call from. Basically I have a network of websites, and I am creating a post on one site from a different site.
  • I am creating a custom post_name (slug) value for the page.

So, when I add the page programmatically, everything in the database looks just as if it were added via the WordPress Admin Area. I can even go to that website's WordPress Admin area, and edit the post. However, if I go to view the page it throws a 404. Now, if I "update" the post from the WordPress Admin area, the 404 goes away.

If I create a post programmatically and create it from the WordPress Admin area, and then look at the database columns, they are the EXACT same values (or appear to be). Why would one work, and one wouldn't? Also I'm using permalinks (nothing custom, just one of those predefined choices).

This is the wpdb insert call I'm using...

$post = array(
    'post_author' => 1,
    'post_date' => date('Y-m-d H:i:s'),
    'post_date_gmt' => date('Y-m-d H:i:s'),
    'post_content' => '',
    'post_title' => 'My Page',
    'post_name' => 'my-page',
    'post_excerpt' => '',
    'post_status' => 'publish',
    'comment_status' => 'open',
    'ping_status' => 'open',
    'post_modified' => date('Y-m-d H:i:s'),
    'post_modified_gmt' => date('Y-m-d H:i:s'),
    'post_parent' => 0,
    'post_type' => 'page',
    'comment_count' => 0

    array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' );

Permalink Error

I thought I was using a basic permalink, but forgot that I had used a custom permalink structure on this particular website.

The custom permalink I used was %postname%.

I switched it to the month and name option and now everything works. Sorry for the bogus question.

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.