0

I just noticed PHPStorm "complaining" that my first argument to wp_insert_post is an object, where an array is expected.

I also noticed that there's no such complaint with wp_upate_post.

It would be somewhat of a pain to refactor my code to use an array for every call to wp_insert_post, and furthermore the mismatch doesn't seem to cause any issues -- I suppose it is being automatically cast by PHP (?)

Is this something I ought to address or is it safe to leave as-is? I guess this might be more of a PHP concern than WP.

It did seem odd to me that two closely related functions like this have different argument type specifications.

2

Array's are technically preferred for both, but it actually doesn't matter that much. PHP is not casting the object to an array, worpress is. wp_insert_post(), like a lot of wordpress functions, runs it's "array" parameter through wp_parse_args() which will cast an object passed to it to an array and will always returns an array.

You should probably address it in case wordpress ever released an update where they stopped casting the object for you and PHP will throw a fatal error if you try to use an object as an array. But frankly, that casting probably isn't super likely to be removed any time soon.

If you did want to fix it, all you would have to do is replace

wp_insert_post($object);

with

wp_insert_post(get_object_vars($object));

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.