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

93 questions with no upvoted or accepted answers
Filter by
Sorted by
Tagged with
3
votes
1answer
366 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 ...
3
votes
1answer
457 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
0answers
57 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 ...
2
votes
1answer
204 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
52 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
36 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
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
156 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
147 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
0answers
140 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
546 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
157 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
181 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
1k 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
0answers
104 views

Random post_id at wp_insert_post

How I can use an absolute random post_id in wp-admin at wp_insert_post? I need stored in wp-admin a unique random post_id, but not I have only: 2210, 2212, 2214. As I understand it, this will not work ...
0
votes
0answers
21 views

Auto-Select Parent Category as Primary

I am using Yoast SEO Premium to handle the selection of Primary Categories. I am currently working with a Listing (listing) custom post type with a hierarchal taxonomy of Listing Category (listing_cat)...
0
votes
1answer
29 views

wp_insert_post($post), add an array of values with update_post_meta

i try to store an array in multiple custom fields via post creation (wp_insert_post). The array is $event_test and the key is event_test on the meta_input. On form submit, the custom field is shown, ...
0
votes
2answers
20 views

How to prevent wp_insert_post from creating a new post every second?

I am trying to create a simple plugin that creates only one new post. The code I have written creates a the same new post every second. Below is the code I have. What would need to be added so it ...
0
votes
0answers
27 views

Programmatically Upload Image to Media & CDN

I'm trying to create a feature that allows my website to: Crawl through a CSV file and programmatically create a post Upload a featured image based off a URL from the CSV file to the Media Library (...
0
votes
0answers
26 views

WC attributes and terms cannot be added with the first pass of the script

In general, I am trying to insert/update post of type 'product' with attributes and terms into tax_input. It does not happen as expected. Firstly I tried to use wp_insert_post with 'tax_input' key ...
0
votes
0answers
29 views

Updating failed message when a wp_insert_post function is hooked to save_post hook

I was creating a function which creates a new custom post type with the category or taxonomy name if it was created from the post editor. I am simply using the wp_insert_post function with the ...
0
votes
1answer
69 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
16 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
37 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
71 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
157 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
196 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
3answers
1k 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
87 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
29 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
364 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
23 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
142 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
163 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
313 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
411 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
578 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
261 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
44 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
149 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
93 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
20 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
838 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
135 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
859 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 ...