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! :)
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' ...
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 ...
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) {
...
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 = ...
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 ...
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 ...
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 ...
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 ...
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 ...
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']...
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 ...
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 ...
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 ):
"...
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. ...
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 ...
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!
$...
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->...
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']);
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 ...
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 ...
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('...
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/
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'], '...
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.
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 ...
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.
...
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)...
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 (...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
wp-insert-post × 438custom-post-types × 56
posts × 45
post-meta × 40
php × 37
plugin-development × 25
front-end × 20
plugins × 18
custom-taxonomy × 18
custom-field × 17
save-post × 17
wpdb × 16
wp-update-post × 16
ajax × 14
terms × 13
forms × 13
hooks × 12
post-thumbnails × 10
functions × 9
database × 9
actions × 9
pages × 8
taxonomy × 8
slug × 8
wp-query × 7