This function inserts posts (and pages) into the database. It sanitizes variables, does some checks, fills in missing variables like date/time, etc. It takes an array as its argument and returns the post ID of the created post (or 0 if there is an error).

learn more… | top users | synonyms

7
votes
3answers
18k views

How do I set a featured image (thumbnail) by image URL when using wp_insert_post()?

While looking through the function reference entry for wp_insert_post(), I noticed that there's no parameter in the array it requires which will allow me to set the 'Featured Image' for a post, ...
5
votes
3answers
7k views

Attaching taxonomy data to post with wp_insert_post

I am implementing (trying) a front end posting system which shows taxonomy data in several dropdown select fields, each of the dropdowns is named by using the "name" $arg in wp_dropdown_categories. ...
5
votes
1answer
2k views

wp set object terms vs wp set post terms

Examining the following sample code snippet from the WP codex, $post = array( 'ID' => [ <post id> ] //Are you updating an existing post? 'post_author' => [ <user ID> ] //The ...
4
votes
3answers
496 views

Running WP Cron on specific local time

The scenario is to run insert_post function every midnight (00:00) of local time. Must run daily on weekday. function add_daily_task(){ if((date('l', time()) != 'Saturday') || (date('l', time()) ...
4
votes
1answer
797 views

wp_insert_post incorrectly escapes HTML comments when they include tags

I'm working on writing a script to import data from a custom CMS into Wordpress. As part of this, I need to import the post content exactly, without modifying anything. However, some of the posts ...
3
votes
1answer
753 views

Create posts on user registration

How can I create let say 3 posts after the user finish the registration ? The ideal outcome would be to create 3 posts using the user as the author of the posts and with some pre-defined values. ...
3
votes
1answer
1k views

Does wp_insert_post validate the input?

If I use the wp_insert_post in a plugin, will I have to check the content for SQL injection problems and XSS or will the wp_insert_post function do this for me?
3
votes
2answers
338 views

PHP/Manual use of images? - 2 Questions

I've made a quick PHP script/page that uses the wp_insert_post() function to insert a post directly. And this works great. The post I am manually adding has an image in it, so I firstly ...
3
votes
1answer
2k views

When and Where to use wp_insert_post()

I'm building a function that creates a group of pages with a common parent ID. If I run wp_insert_post() on a parent ID twice, does the function create the pages twice while altering the slugs? Or am ...
3
votes
1answer
945 views

Using wp_insert_post() with Networking enabled

I'm working on a script that will drop articles directly into the wordpress database, using wp_insert_posts(). However, I have networking enabled and the wp_insert_post() reference page has no ...
3
votes
2answers
439 views

wp_insert_post breaks rewrite rules

I'm using wp_insert_post to programmatically add content to a site. This works fine with posts, categories and tags, but not with pages. When I attempt to add a page (i.e. post with post_type=page), ...
2
votes
2answers
357 views

Prevent empty Post Title on form submit via front end post (wp_insert_post_)

I am creating a post from the front end using (shortened for brevity); if (empty($_POST['my_title'])){ echo 'error: please insert a title'; } else { $title = $_POST['my_title']; } ...
2
votes
2answers
592 views

wordpress sanitize array?

I have a custom post form that sends data to a page which uses wp_insert_post to create a post. I can easily sanitize most data, but I have some troubles with my tags, I retrieve in an array: $tags = ...
2
votes
2answers
184 views

wp_insert_post() Callback?

After I added a post by wp_insert_post(); Any callback after I added ? I need permalink of added post. I have idea only search by title and post-type (I use custom post-type in my case), But any ...
2
votes
1answer
655 views

How do I Import an RSS feed as Wordpress posts without duplicates?

I have an XML feed url I am now using this code to create new Custom Post Type Posts from an rss feed in my functions.php file: /* | ...
2
votes
1answer
1k views

save_post + insert_post = infinite loop

I'm trying to automatically create a new post when a post containing a certain custom field is saved - hooking an insert_post function into save_post. This generates an infinite loop. I found some ...
2
votes
1answer
224 views

Endless loop with wp_insert_post and wp_update_post

I have an array $unqiues which contains URL's, I loop over the array with the code below to get the URL titles, insert a new post, update the post meta. Then I change the the posts title and name to ...
2
votes
3answers
1k views

How to set featured image to custom post from outside programmatically

I am trying to fetch and insert images outside the wordpress environment to a custom post via PHP. How to move/upload that image to wordpress upload directory year date folder format just like ...
2
votes
1answer
715 views

Inserting Hundreds of Thousands of Posts at Once

I have a project in which I create an array of 200k+ lines w/ 25 fields that run the wp_insert_post and update_post_meta. To start it processes pretty fast the slows down a lot after 10k records or ...
2
votes
2answers
167 views

Is there a faster way than wp_insert_post to add content to a blog

I've written a plugin which retrieves an xml file, parses and creates a series of short posts from the content. Although each post is very short (sometimes just a single sentence, never more than a ...
2
votes
1answer
50 views

Wordpress tax_input only if logged in

I am working on a wordpress plugin where a save lead data by forms in my website. When a user submits the form the following code will run so a post in post type "lead" will be added. Almost ...
2
votes
1answer
58 views

Can we have a post without a slug?

When I insert a post I've found that there is a slow call to wp_unique_post_slug from within insert_post (In the database I'm using to test there is 155,000 posts - live would have considerably more). ...
2
votes
2answers
240 views

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

I want to use WP-CLI to create some new posts with custom taxonomy terms assigned. The challenge is that wp_insert_post's tax_input argument only accepts arrays, which I would have to specify on the ...
2
votes
2answers
874 views

Adding post thumbnail in programatically inserted post

I want to insert post programatically so here is the code to add one: global $user_ID; $new_post = array( 'post_title' => 'My New Post', 'post_content' => 'Lorem ipsum dolor sit ...
2
votes
1answer
556 views

Programmatically insert hierarchical terms & set terms for post causes glitch? [duplicate]

I am inserting an array of terms into my custom taxonomy programmatically. Some terms have parents/children. After each term has been entered, I then insert an array of posts into my custom post type. ...
2
votes
3answers
697 views

How to prevent duplicate slugs for wp_insert_post?

I have a function that creates a page when I create a post (so as to construct parent-child relationship trees). I'm using the $title of the post to create the page and this also creates a duplicate ...
2
votes
1answer
584 views

How many 'wp_insert_post' calls can be performed in one shot, in a very long 'for' loop?

I'm working on a plugin which has bulk post inserts using a spreadsheet. This spreadsheet can have multiple thousand rows, each row corresponding to a post. I'm parsing this spreadsheet, looping over ...
2
votes
4answers
2k views

wp_set_object_terms() Fails to Set Terms

Okay guys, here's the scenario. I'm am trying to setup a function that will automatically duplicate a post (when published) over to another post type. So, a regular blog post is published, and when ...
1
vote
2answers
1k views

What hook should be used to programmatically create a post only when master post is updated?

Pippin and Jean-Baptiste Jung have very good tutorials on how to programmatically create content when a new post is published using these four actions hooks... publish{post_type} ...
1
vote
2answers
526 views

Faster way to wp_insert_post & add_post_meta in bulk

I have a csv file that I want to insert that consists of ~1,500 rows and 97 columns. It takes about 2-3 hours to do a full import and I'd like to improve this if there is a way. Currently for each ...
1
vote
4answers
4k views

Proper formatting of post_date for wp_insert_post?

What is the proper way to define the post date when submitting a post from the front end using wp_insert_post (Trac)? My snippet now is publishing with the mysql time... if (isset ($_POST['date'])) ...
1
vote
5answers
1k views

Insert Wordpress page via external (cron) script?

I'm trying to write a php script that I can run out of cron to create new Wordpress pages. Unfortunately, I can't find any documentation on how to do this. I'm using the WP scripts rather than ...
1
vote
1answer
100 views

What is considered the post's creation date for wp_insert_post?

I have the following hook which sets a default "hotness" for a post. For context, hotness is a meta_value saved to the post, and changes every time someone upvotes or downvotes the post. I use this ...
1
vote
1answer
120 views

HTML Entities in Post Title

I'm inserting posts into wordpress using a script which is loaded through CRON. Before inserting each post I am checking whether the post with the same title exists or not. Below is the snippet I'm ...
1
vote
1answer
1k views

insert post & Upload post thumbnail from the front end using ajax

i have searched & searched for an explenation and have found some answers that seem to be working for other people on this topics but i havent been able to get working. This is my "form" <div ...
1
vote
3answers
298 views

Is it possible to create a post using a metabox?

I am trying to create a post (well, in the end, multiple posts) from a metabox using the 'save_post' hook. I executed the script once, creating 3900 or so new posts before I crawled into posts.php and ...
1
vote
1answer
164 views

wp_insert_post not updating custom taxonomy selected if logged in as a subscriber

Here is my Code, it works fine When i try to post data from front end if Logged in as admin, but if i logged in as a subscriber code works fine but it is not inserting the taxonomy term i select, here ...
1
vote
1answer
192 views

Inserting post, thumnail and custom fields with wp_insert_post

I just want to add custom post, a thumbnail of it and custom fields. Here is my code what I am try is to add post, thumbnail and one custom field. But I not getting the idea how to upload the ...
1
vote
1answer
238 views

add_menu_page Callback Function: Skip page content?

I want to have a menu item that will, on-click, add a page with certain arguments. In it's simplicity, I have: function av_subscribe_create_menu(){ // Create top-level menu add_menu_page( 'Add ...
1
vote
1answer
508 views

duplicate posts when trying to update a post using the wp_insert_post.

On an empty wordpress database, I insert one sample post using the wp_insert_post. Since it's an empty database, the first post I add ends up with id 1. And I check that in the wp_posts, everything ...
1
vote
2answers
4k views

WP insert post and custom taxonomy

I have create a custom post type in my WordPress attached with a custom taxonomy. The post collect date from the from end of the web site by using wp_insert_post(). This is my Custom post ...
1
vote
2answers
4k views

How to use wp_insert_post to update meta box?

I'm trying to use wp_insert_post to create a front-end submission form so visitors can create a post AND update the meta box fields of that post -- not the custom fields. For example, when I use the ...
1
vote
1answer
26 views

tax_input argument for wp_insert_post()

I'm trying to insert custom taxonomies using wp_insert_post() and the tax_input argument, but it doesn't work. I want to set a post to a custom taxonomy ('types') and a category ('cars') $new_post = ...
1
vote
2answers
68 views

using wp_insert_post inside of custom meta box

This is kind of a unique situation. I have an invoicing custom post type that when it is marked as "paid" I want to then generate another custom post type. I have a custom meta file that handles the ...
1
vote
1answer
223 views

WP_INSERT_POST issue on Wordpress Multisite

Im having a hard time on fixing this issue of mine. I have a plugin where it creates 6 pages at default for new wordpress multisite sites. Im using the following code. function ...
1
vote
1answer
378 views

After wp_insert_post(), date_i18n() and date() outputs are adjusted to GMT

I am having a very peculiar issue on a customer's server (vps). The code below works just fine on my servers. Basically, a specific unix timestamp is formatted correctly and as soon as ...
1
vote
1answer
2k views

wp_insert_post or wp_set_post_terms do not save taxonomy, but wp_set_post_terms does

I have a litlle problem. I have a custom taxonomy 'rodzaj' with value i.ex. obrazek. I use this code to add post: $postArray = array( 'post_status' => 'publish', ...
1
vote
1answer
93 views

Frontend tag edit/submit form

I would like to have a separate page to submit/edit tags. Any user can edit or submit tags. But it should be published only after admin approval. Is there any function available like ...
1
vote
1answer
169 views

post_content sanitization / transformation process

What kind of transformation does the post_content go thru when wp_insert_post inserts a post_content into wp_posts programmatically? I'd like to be aware of this before I process 50000 recs into ...
1
vote
2answers
586 views

Making sure wp term relationships records are unique

Which' WordPress function is used in programmatically associating the term taxonomy IDs with post IDs? And does that function have internal mechanisms to make sure no duplicate entries are allowed ...