1

I've got the following code in the 'functions.php' file of a wordpress theme

$ppost_data = build_post( $pID, $pcontent, $pname, $ptitle, $pstatus, $ptype, $pset_comment_status );

wp_insert_post($ppost_data);

and here's the 'build_post()' function:

function build_post( $pID, $pcontent, $pname, $ptitle, $pstatus, $ptype, $pset_comment_status ) {
return array(
    'ID'=>$pID,
    'post_content'=>$pcontent,
    'post_name'=>$pname,
    'post_title'=>$ptitle,
    'post_status'=>$pstatus,
    'post_type'=>$ptype,
    'comment_status'=>$pset_comment_status,
    'post_author' => 1
);

The variables are all set, and all of the data is in the $ppost_data array correctly, but the page isn't being added. No errors are being thrown. Any ideas?

  • where does $pID come from? If it's not the ID of an existing post, the function will fail. – Milo Jan 10 '14 at 22:39
  • Milo beat me to it. If you pass an id, you are implying an update. Don't pass one to tell the function to create a post. – Andrew Bartel Jan 10 '14 at 22:39
  • Milo wins! Thanks for your help guys. Milo, if you'll post an answer, I'll accept it. – KingRichard Jan 10 '14 at 23:37
2

When inserting a new post, you must omit or leave the ID parameter empty, or the post will not be inserted. You can only set the ID if you are updating an existing post with that ID.

See wp_insert_post in Codex for more information.

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.