1

if I run the following code it returns me an int(0). Wordpress creates the Post but with an empty post_title although the $name variable is filled.

$postID = wp_insert_post(array(
    "post_title" => $name,
    "post_status" => "publish",
    "post_type" => "mycustomposttype",
), true);
var_dump($postID);

1 Answer 1

7

According to the notes on the codex wp_insert_post()

post_title and post_content are required

If you want the content to be empty, you can write your wp_insert_post args array thus:

$postID = wp_insert_post(array(
    "post_title"  => $name,
    "post_status" => "publish",
    "post_content => " ",
    "post_type" => "mycustomposttype",
), true);
1
  • Also incorrect encoding could leads same result. Sanitize fields always.
    – boctulus
    Commented Sep 1, 2023 at 2:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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