Questions tagged [wp-insert-post]

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).

90 questions with no upvoted or accepted answers
Filter by
Sorted by
Tagged with
3
votes
1answer
226 views

Speeding Up Bulk Post Creation - wp_insert_post & update_post_meta

I am creating posts (variable products) using (wp_insert_post) function. An Example, I have 9 colors, 9 sizes. Which makes 9×9 = 81 total variations for 1 product, which is 81 times below function to ...
2
votes
1answer
425 views

How can I stop wp_update_post messing up HTML example code?

I'm showing little pieces of example HTML/PHP code on the frontend (with syntax highlighting). The entries are displayed as FAQ custom posts, and have a plugin (CMS Tree page) to alter the menu order, ...
2
votes
1answer
196 views

Efficient way to save a lot of meta data

im currently developing a plugin that will read an external xml, and convert the data to a custom post type. Each post needs around 50-60 meta_fields. Right now im looping through each piece of needed ...
2
votes
1answer
1k views

Organizing uploaded media with wp_insert_post() and wp_handle_upload according to time parameter

I'm retroactively creating a post with wp_insert_post() and attaching a featuring image to it. I need the image to go into the folder that corresponds to the post's publishing date. I'm using time ...
1
vote
1answer
28 views

Check if post is added manually or through wp_insert_post()

I might not be thinking this right but here is my situation: I have a custom post type called events. Some of my events are imported from an API and inserted through wp_insert_post() (in a plugin), ...
1
vote
1answer
46 views

Don't insert if permalink is the same?

I have a foreach loop inserting posts. And I already have statements to check if post already exists by unique meta values. But somehow, often when site is loading slowly, something strange happens ...
1
vote
0answers
30 views

When User Meta amended update Title and Slug of Post programatically

I have this code below which works perfectly, i.e. when adding a new User a post is programatically created in my CPT. add_action( 'user_register', 'add_member_employees' ); function ...
1
vote
0answers
49 views

Is there a better way to programmatically insert content into a page?

During activation of the plugin I'm writing, I insert a custom page. I can add post_content using the wp_insert_post command, which seems like a reasonable way to add a limited amount of content. For ...
1
vote
0answers
37 views

Auto-create a post with last 10 images in WordPress directory

I want to create something to help me auto-create a post with 10 last images hosted in some directory (in my server or WordPress), this code I found helps to post an image but not many images if ...
1
vote
0answers
144 views

Custom Taxonomy isn't working from frontend post submitting

hi, i am working on a video sharing site project! I have Created a Template file where people can submit their post as video! Everything was working very good, but the custom Taxonomy ponstars isn't ...
1
vote
0answers
121 views

Compare old meta with new post meta

How do I compare old meta what was already stored in the database with the new meta data what was just set by the user? For example if you want to keep track of how many times a button is clicked. ...
1
vote
1answer
581 views

Cropping an image before inserting into a post

I've got a frontend post form, and I've been using this PHP code to allow uploading an image and to insert it into a post. // image upload and attatching to post if ( ! empty( $_FILES ) &&...
1
vote
0answers
138 views

Attaching taxonomy data to taxonomy with wp_insert_post

By WP User Frontend I create a registeration form with some checkboxes, and use wp_insert_post and update_user_meta for create a new post (post type pavilion) on user registration.I want when checked ...
1
vote
0answers
538 views

Frontend Post with JQuery AJAX to Php Issue

My problem is the following. I'm trying to make users publish custom post_type by frontend through a form like this: <form id="msform" enctype="multipart/form-data"> <fieldset id="...
1
vote
0answers
1k views

Upload featured image from front end using media-upload.php

I'm trying to use admin/upload.php to upload featured image from front end form. All I have to do is put below code in the front end form <div id="postimagediv" class="postbox "> <div ...
1
vote
0answers
1k views

Create post using Ajax

I want to create post using Ajax . I had followed the process from Insert Post using Ajax But I couldn't make it work. Here is what I did. Please see what I did wrong as I am just a learner. EDIT ...
1
vote
1answer
138 views

Permalink for custom post type

I have created a custom post type called job. Posts are generated with wp_insert_post(), and that works perfectly fine. My problem is that the permalinks to those posts are either wrong or I have done ...
1
vote
0answers
172 views

Post author for revision not being set on update/insert

I'm trying to programmatically update a custom-type post with revisions enabled via form, and everything seems to be working fine except the post_author field for the revision is being set to 0 ...
1
vote
0answers
948 views

Prevent Duplicate Attachment When Using wp_insert_post

I am trying to use wp_insert_attachment while using wp_insert_post. It's successfully creating the post and attachment. It have no problem if I only use the wp_insert_post function, but when I ...
0
votes
1answer
13 views

how to create a category with wp_insert_post and post_category

I am trying to create a post with php and its wordpress functions. I am using wp_insert_post () but I can not assign a custom category type text to the post nor create it I have this code: ...
0
votes
1answer
13 views

Creating categories while importing via CSV

I have a CSV that I am importing via a custom plugin I made. This will create a CPT that has a few ACF fields. This CPT is called Formula, and it has a taxonomy associated with it called formula_type. ...
0
votes
0answers
18 views

wp_inset_post not adding taxonomy (using wp_set_object_terms)

The following runs through a cron job and successfully creates and publishes the post to a custom category. However, it does not attach a custom taxonomy (ap_job_status) term (ID 3) to the post. $...
0
votes
0answers
44 views

get_page_by_title() not working as expected

I'm looping through an API with jobs and creating new posts from it like this: //fetch jobs and loop through them... // check if post title exists, if not add it. if ( ! ...
0
votes
1answer
76 views

Meta inserted through wp_insert_post gets messed up

I'm extending the Woocommerce Product CSV Import functionality to import more data. I'm inserting a CPT with 2 metadata, a string (URL) and a bool, and the post_meta inserted gets messed up. It ...
0
votes
1answer
38 views

Can't publish post using ajax

I have an issue, i create three input in frontend(title, body, and name) I process the value using wp-ajax, and in my ajax function I use wp_insert_post to create a new post. if I put '...
0
votes
0answers
21 views

Generate post content from meta fields

I have a custom post type (match). Each posts on this CPT has one ore more custom fields. Is it possible to automatic generate the 'post content' with the value of my custom fields on these posts? ...
0
votes
1answer
102 views

tax_input in wp_insert_post partly not working

I'm inserting a post via wp_insert_post() and in the $args the tax_input looks like this: 'tax_input' => array( "programmas" => 'Modern Family', "zenders" => 'ABC', ), But what is ...
0
votes
2answers
567 views

Set Primary category using the Yoast SEO plugin

function set_primary_on_publish ($post_id) { global $post; $categories = get_the_terms( $post->ID, 'category' ); // wrapper to hide any errors from top level categories or products ...
0
votes
1answer
74 views

wp_set_post_terms() seems very slow

Is it normal for wp_set_post_terms() to be very slow? When wp_insert_post() it takes about 1 second. Adding 4 wp_set_post_terms() 7 - 10 seconds to the operation. Pseudo code example: $values = ['...
0
votes
0answers
26 views

Create posts by any logged in users

I want to allow logged-in any users to create posts. For this purpose I created a form <form> <div class="form-group"> <input type="text" class="form-control" id="...
0
votes
1answer
250 views

wp_insert_post not inserting post

I'm trying to insert a post when a user (aka me) clicks on the button (this button is inside a meta box on the admin dashboard). This doesn't happen and I don't know why. When I put the content of my ...
0
votes
0answers
61 views

Deleting custom post type from external API via wp_delete_post

I've been working with an external API to pull in data and create what's essentially a job board via custom post types with wp_insert_post. My concern is that once these jobs/custom posts are removed ...
0
votes
0answers
22 views

Published page but it is page not found

I have published page with post type abc, but the page is not found. If I keep post type, then it works fine. I insert post data and publish through query: add_action( 'init', 'my_cpt_init' ); ...
0
votes
0answers
57 views

Programmatically create new post from a metabox button

Given a classic course-lesson structure, created by custom post types, I want to add a new empty lesson for a course, by using a simple html form on a metabox, in the course edit screen. I think I ...
0
votes
0answers
21 views

Checkboxes to automatically create pages when I activate a theme

So, I want to create pages automatically on theme activation. This seems kinda easy with wp_insert_post(). The problem is: I need to select only certain pages to be created. Is there a way to do this?...
0
votes
0answers
139 views

Meta keys won't add using wp_insert_post

I have the following code, I am attempting to insert a new post with the 2 meta fields attached, the post is being inserted but the meta isn't there. Can't seem to spot anything wrong here, so what's ...
0
votes
0answers
207 views

Is it possible to use 'wp_insert_post' function within a for loop?

I want to insert as a custom post type some data fethced from an API, I store that data and then pass it trough a for loop, then I use the loop data to create an array to pass it to wp_insert_post ...
0
votes
0answers
64 views

How to insert post 6 times after user register like ask.fm

I have a question and answer site like ask.fm. On ask.fm when you register a new account you found that you have 6 questions (posts). I need to make the same. Whenever a user registers I need to add ...
0
votes
0answers
305 views

Insert Multiple Posts from JSON Results

I am working on a custom function to insert posts from a JSON feed. The JSON feed produces results as it should, but I am unable to get the wp_insert_post() function working properly, I assume ...
0
votes
2answers
410 views

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

I need to insert about 300 posts into the DB programmatically. Usually i use wp_insert_post to do this job with one record each time, but this time I don't think it'll work well with hundreds of rows....
0
votes
1answer
239 views

Adding user meta to post meta wp_insert_post() not working

i'm trying to add a users meta field to a posts custom field when inserting a post using wp_insert_post() but it isn't working. I've tried replacing the $branch with a simple string and it works but ...
0
votes
0answers
36 views

Creating a frontend blog post form for logged in users only

I recently tried to make a form for users that are logged in to be able to add a post from the frontend to their profiles. The problems I have found are the following: ANYBODY can post from it, even ...
0
votes
1answer
88 views

How can I automatically add a post with Latin characters?

I would like to add a post in Wordpress with cURL (php) and I have this string to add as a content: $text = "Trovato, assieme a Massimo e Anna, in una cantina di Via Barrili a Milano, con un'...
0
votes
1answer
81 views

Get post meta after insert post

I am trying to get post meta after post is being inserted via code but its not working. Post meta not showing: $id = wp_insert_post( $post_args ); $zip=get_post_meta( $id,'baseSalary',true); print_r(...
0
votes
0answers
19 views

automatic creation of custom post type articles

I have an external database besides WordPress which has a table with genealogical data. In WordPress, I have created a Custom Post Type (CPT) and for these genealogical records. There is also a ...
0
votes
0answers
775 views

500 error after wp_insert_post

The insert post works correctly, but then afterwards get a 500 internal server error for some reason, tested on a simple localhost site on my computer but error does not occur. :- global $user_ID; $...
0
votes
1answer
112 views

insert category from form in a post

when the category comes from a form, how I can use in wp_insert_post? $my_post = array(); $my_post['post_author'] = $userid; $my_post['post_title'] = $name; $my_post['post_name'] = str_replace(' ',...
0
votes
2answers
682 views

How to insert post from external php file?

I have this php file on the root directory, and i seen this example for inserting post from external php file but for some reason it doesn't work for me, wp_insert_post() always returns 0 So whats ...
0
votes
0answers
383 views

switch_to_blog($blog_id); still writing to main blog

I'm creating new blogs (wpmu_create_blog) in a for loop, What I am trying to achieve now is to insert posts into each newly created blog using switch_to_blog. The problem is wp_insert_post still ...
0
votes
1answer
401 views

How to Create Content Remotely

I'm trying to help populate a newly setup wordpress website and am trying to write scripts to do automated posting of a pre-processed archive of e-mails as a series of posts and comments. I'm ...