Skip to main content
31 votes
Accepted

How to set post slug when using wp_insert_post();?

The parameter to insert a custom slug is: 'post_name' => 'my-custom-slug' Not post_slug as one would think! :)
mayersdesign's user avatar
12 votes

Check if Post Title exists, Insert post if doesn't, Add Incremental # to Meta if does

A more updated method may use the post_exists() function like so: if( isset( $_POST['submit'] ) ){ $post_title = sanitize_title( $_POST['post_title'] ); $new_post = array( 'post_title' ...
Tim Hallman's user avatar
11 votes

tax_input not working wp_insert_post

It turns out that tax_input does not work if a user does not have the capabilities to work with a custom taxonomy: wp-includes/post.php (wp_insert_post): So either add the correct caps or use ...
Eduardo Marcolino's user avatar
9 votes

wordpress sanitize array?

I needed a recursive sanitation, so here's my solution: /** * Recursive sanitation for an array * * @param $array * * @return mixed */ function recursive_sanitize_text_field($array) { ...
Broshi's user avatar
  • 191
7 votes

wp_insert_post returns int(0)

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 = ...
Tunji's user avatar
  • 2,961
6 votes
Accepted

wp_insert_post works, but the post isn't visible in the admin post list or front end

The problem is $postarr["post_status"] = "published"; you should change it to $postarr["post_status"] = "publish"; // Set it to 'publish', not 'published' Now, this is a bit off topic, but you may ...
Greeso's user avatar
  • 2,204
5 votes

wp_insert_post extremely slow on big table, direct query very fast

you should revers the code you should add this befor the import wp_defer_term_counting( true ); wp_defer_comment_counting( true ); don't set it false and after the import you set it true you must ...
Abdalla Mohamed Aly Ibrahim's user avatar
5 votes
Accepted

How to allow data:image attribute in src tag during post insert?

Thanks to naththedeveloper from StackOverflow. His answer worked for me. Well, this was a nightmare to find, but I think I've resolved it after digging through the WordPress code which hooks in ...
Earth's user avatar
  • 181
4 votes

tax_input not working wp_insert_post

As you said in comment that plugin will do the filters and don't have hook, I think plugin must have wp_insert_post hook anyway. Otherwise how do you insert the post ? I am answering this after so ...
Tuhin A.'s user avatar
  • 175
4 votes

Adding post thumbnail in programatically inserted post

use the function set_post_thumbnail, //$file is the path to your uploaded file (for example as set in the $_FILE posted file array) //$filename is the name of the file //first we need to upload the ...
Aurovrata's user avatar
  • 1,346
4 votes

How to update custom fields using the wp_insert_post() function?

You can add 'meta_input' element on the post parameters as an array of post meta values keyed by their post meta key <?php $my_post = array( 'post_title' => $_SESSION['booking-form-title']...
Droid Sheep's user avatar
4 votes

Slash is being stripped in my post_name when using wp_insert_post

You can use this plugin for .html extension https://wordpress.org/plugins/html-in-url/ And for slashes you should create page of 'keyword1' and 'keyword2' and make keyword2 as a parent page. then ...
faisal's user avatar
  • 41
4 votes
Accepted

Contact Form 7 to featured image

Thanks @Judd Franklin for the directions. I was also missing $submission->uploaded_files();. Here is the working code for those who are looking for the same answer: function ...
Pipo's user avatar
  • 609
4 votes
Accepted

Not Able to Insert Taxonomy Term Using wp_insert_post()

Few points come to mind: There is a typo in "post_type" => "'eyeglasses" (extra single quote). It should be: "post_type" => "eyeglasses". Try putting the $term instead of array( $term ): "...
Futuritous's user avatar
3 votes

How do I pass an array as an argument to a WP-CLI command?

A solution inspired by @Saulo Padilha . <- the only person on the whole internet taking this cr*p seriously 😂 Add this snipped to functions.php , set the user argument, and JSON input should work. ...
Davey's user avatar
  • 198
3 votes

wp_insert_post extremely slow on big table, direct query very fast

This will insert posts directly, and a fair bit faster than wp_insert_post. However, generating the guid does not work yet. You have to be a lot more careful with the input you pass in, and it won't ...
Joel M's user avatar
  • 427
3 votes

Faster way to wp_insert_post & add_post_meta in bulk

Important note about 'SET autocommit = 0;' after setting autocommit = 0 if the script stops execution (for some reason, like exit, fatal error or etc...), then your changes WONT BE SAVED IN DB! $...
T.Todua's user avatar
  • 5,850
3 votes
Accepted

Can I trigger the publish_post hook by using wp_insert_post?

It's triggered in wp_publish_post() that calls: wp_transition_post_status( 'publish', $old_status, $post ); that fires up action calls, dynamically with: do_action( "{$new_status}_{$post->...
birgire's user avatar
  • 67.8k
3 votes

Get data from wp_editor()

$content = ''; $editor_id = 'mycustomeditor'; wp_editor( $content, $editor_id ); /** If you want to get data wp_editor value from POST **/ print_r($_POST['mycustomeditor']);
Aouadi Mohamed Ali's user avatar
3 votes

How to use wordpress $polylang->model->set_post_language in custom php code?

Turns out that the language relationship is simple and set in just a few functions: // Insert the EN post which returns the EN post id. $post_data_en = array( // add EN post data like the title and ...
Barklem's user avatar
  • 41
2 votes

Proper formatting of post_date for wp_insert_post?

came across thru google. i know its old but there is no definite answer. wordpress code uses current_time( 'mysql' ) to save date/time in wp_update_post function! this will generate the desired date ...
Nic Bug's user avatar
  • 231
2 votes

Running WP Cron on specific local time

First, make sure your WordPress date and time settings are correct as well as your local server time. Then use this code: $datetime = new DateTime('midnight', new DateTimeZone(get_option('...
Tyler's user avatar
  • 131
2 votes

Check if Post Title exists, Insert post if doesn't, Add Incremental # to Meta if does

@Tim Hallman nice answer. Use the function post_exists() to check post exist or not. More details visit https://developer.wordpress.org/reference/
maheshwaghmare's user avatar
2 votes
Accepted

wp_insert_post custom taxonomy

wp_set_post_terms() function will only work on the native post type. For a taxonomy on a custom post type use wp_set_object_terms(). Change your code to: wp_set_object_terms( $pid, $_POST['cat'], '...
C Sabhar's user avatar
  • 409
2 votes

wp_insert post doesn't work

Remove the date param or use the right format for the time stamp, like date('Y-m-d H:i:s'), But it is not necessary, WP use the current timestamp on the insert post time.
bueltge's user avatar
  • 17.1k
2 votes
Accepted

wp_insert_post() is returning the correct post ID, no failure, but the post content does not get updated

Don't bother fixing your AJAX callback, there's already a REST API endpoint that's well tested and does all of this out of the box: example.com/wp-json/wp/v2/zen_page So lets enqueue a helper script ...
Tom J Nowell's user avatar
  • 60.6k
2 votes

Create posts without login from frontend

You should not allow anonymous users to publish anything on your website without authentication. If you need to store custom data that is specified by users, you should use the custom fields instead. ...
Johansson's user avatar
  • 15.4k
2 votes

Create posts without login from frontend

Non Plugins: Look at wp insert post args: https://developer.wordpress.org/reference/functions/wp_insert_post/ You should create html input for some of those $args. When post submitted (if isset post)...
Asisten's user avatar
  • 79
2 votes

Auto-generated posts not showing in backend (but being counted!)

Ugggggggh. "post_status" => "published", is incorrect. It should be "post_status" => "publish", This apparently puts posts into a state of limbo in which they're not visible in the list (...
Pekka's user avatar
  • 542
2 votes

How to combine multiple wp_insert_post into one in order to gain better performance

I would advise using wp_insert_post() and putting you text files into a CSV CSV Post Title,Post Content, "title", "content", "title", "content"... PHP $path = "/posts.csv"; //require ...
DesignMonkeyJim's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible