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 parameter in wp_handle_upload function that supposed to move files to the directories according to the parameter value (see in the code - $upload_date - static value for now). But right now the image moves to the current year/month folder ignoring $upload_date (Ex.: Supposed to go 2007/11, goes …
Category: Web

Creating posts with links from a txt file

I have a script which scrapes articles from the web and saves the url to a .txt file. I've created a custom plugin which on activation, loops through the urls and creates a draft post with the content being an embedded article. I'm using the following code in my plugin <?php register_activation_hook(__FILE__, 'my_plugin_activate'); function my_plugin_activate(){ my_plugin_install_site(); } function my_plugin_install_site(){ global $user_ID; $handle = fopen("listOfURLs.txt", "r"); if ($handle) { $count = 0; while (($line = fgets($handle)) !== false) { // process …
Category: Web

wp_insert_post and title not utf8 inserts with empty title?

I'm using wp_insert_post I loop over a text file one row at a time and for each row I create a post. The text is set as the `post_title', for text that is not utf8 the post inserts but with an empty title. Why does that happen, if I'm able to create a post in the backend admin using non utf8 chars it looks like WordPress converts the encoding in the backend. How can I bypass this with wp_insert_post and …
Category: Web

Import JSON feed into Wordpress Post (wp_insert_post) every 24 hours

I am importing a JSON feed using a php file I am then looping through the JSON data and creating/updating a post using the wp_insert_post function. I want to query the JSON feed every 24 hours to sync any new information to Wordpress. I thought having something like zapier hit the page every 24 hours to run the php could work but it seemed there must be a better way to do that in wordpress/php?
Category: Web

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 happening: the "programmas" is correctly inserted but the "zenders" is not. I'm 100% sure that I've spelled these custom taxonomies correctly. When running the function, a post is correctly inserted except for the "zenders" part. I have all the capabilities, because I am the administrator and I've inserted my user id …
Category: Web

Get original post ID after wp_insert_post

I have a plugin with several CPTs (we'll call them CPT-A, CPT-B, CPT-C, and CPT-D). All CPTs have custom fields. In CPT-D, the user is able to make a selection and assign a post in CPT-A, CPT-B, and CPT-C to the current post in CPT-D. Basically linking relevant records to each other. If the post in CPT-A, CPT-B, and/or CPT-C does not yet exist, the user can "quickly" create the post by entering the post's title using a standard text …
Category: Web

Action hook "wp_insert_post" works but not for last imported post

I want to run a custom function whenever posts are imported to my custom post type. I am currently running this code - function on_post_import($post){ global $wpdb; $results = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish'" ); foreach ($results as $result) { // run custom function } $wpdb->flush(); } add_action( 'wp_insert_post', 'on_post_import' ); The problem I am facing is that it does not work for last imported post. So if there are 3 posts …
Category: Web

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 function directly in the php page, the post is inserted. But inside the function, the code doesn't seem to do anything. My PHP add_action('wp_dashboard_setup', 'schedule_synopsissen_setup'); function schedule_synopsissen_setup() { wp_add_dashboard_widget('schedule-synopsissen', 'Synopsissen maken', 'schedule_synopsissen_content'); } function schedule_synopsissen_content() { ?><form action="index.php" method="post"> …
Category: Web

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 be executed. function create_product_variation( $product_id, $variation_data){ $product = wc_get_product($product_id); $variation_post = array( 'post_title' => $product->get_title(), 'post_name' => 'product-'.$product_id.'-variation', 'post_status' => 'publish', 'post_parent' => $product_id, 'post_type' => 'product_variation', 'guid' => $product->get_permalink() ); // Creating the product variation $variation_id = wp_insert_post( $variation_post ); // Get an instance of …
Category: Web

wp_insert_post does not insert string with apostrophes correctly

I'm using wp_insert_post (for several reasons) in a script I'm writing and it's causing any string that has an apostrophe in it to be truncated up to that character (including the character). Here's the code I've got to test it: $question = "If you’re in need, this is who you call"; echo $question."<br />"; $post = array( 'post_title' => $question, 'post_status' => 'publish', 'post_type' => 'question' ); echo $post['post_title']; $the_post_id = wp_insert_post($post); It's weird because when I echo out both …
Category: Web

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 the problem ? and how i can fix it? I'm trying to build a Cron Job file to insert new posts from XML file In the end Thank you! CronJob.php: <?php require_once './wp-load.php'; $new_post = array( 'post_title' => 'My New Post', 'post_content' => 'Lorem …
Category: Web

php wp_insert data on front using a form

I'm using a form on my website to allow user to create posts on my wordpress admin. It works fine, but I have an issue, I don't get the tags meta inside my admin. here is my php code : <?php if(isset($_POST['submit'])){ $err = array(); $err['status'] = true; $output = ""; if(empty($_POST['pseudo'])){ $err['status'] = false; $err['msg'][] = 'Le champ "Pseudo" ne peut être vide.'; } if(empty($_POST['mail']) || !preg_match('/[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}/',$_POST['mail'])){ $err['status'] = false; $err['msg'][] = 'Le champs "Mail" est mal renseigné'; } …
Category: Web

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: require_once 'wp-load.php'; // This is test snippet for learning purposes. // Creates a WordPress Post with code. $post_id = -1; // Set the Author, Slug, title and content of the new post $category=get_cat_ID( 'Samsung' ); $author_id = 1; $slug = 'wordpress-post-created-with-code'; $title …
Category: Web

Is it possible to pin a post in second position from top

as the topic says i wish to keep a post not on top but in second position so that new posts are on top while one of my choice will be just after the latest. Is that possible, can't find a plugin that does it. Edit: I have posts on frontpage 1 by 1 below each other, like a google search result, i want a 'featured' post to stick always below the new/first post which is on top of the …
Category: Web

"transition_post_status" action creating two post with wp_insert_post

What I'm tiring, I've created a custom post-type called the recipe when creating a new post on the recipe, I have inserted a new post on another post-type it working fine but post created two times on every published post function post_create_on_publish_only( $new_status, $old_status, $post ) { if ( ( 'publish' === $new_status ) && 'recipe' === $post->post_type ) { $my_post = array( 'post_title' => get_the_title($post), 'post_status' => 'publish', 'post_type' => 'wpcf7_contact_form', ); $get_post = get_page_by_title(get_the_title($post)); if ( !is_page($get_post->ID) && …
Category: Web

wp_insert_post custom taxonomy multiple Category not added

I have a custom post type jobs and a custom taxonomy jobCategory. wp_insert_post() and it works. When a user selects multiple jobCategory, only one selected category is recorded in the database. please guide me if (isset($_POST['send'])) { $post_id = wp_insert_post(array( 'post_type' => 'jobs', 'post_author' => get_current_user_id(), 'post_title' => $_POST['title'], 'post_content' => $_POST['description'], 'post_status' => 'draft', )); wp_set_object_terms($post_id, intval($_POST['category']), 'jobCategory'); add_post_meta($post_id, 'price', $_POST['price']); add_post_meta($post_id, 'time', $_POST['time']); if (isset($_FILES['file'])) { require_once(ABSPATH . 'wp-admin/includes/file.php'); $uploadedfile = $_FILES['file']; $upload_overrides = array( 'test_form' => false …
Category: Web

Problem on wpdb queries, wp_insert_post, wp_insert_user etc. The query runs twice

Anyone encountered this issue? I have created a custom php script which adds a new post by using wp_insert_post() but everytime I run the code it creates a 2 new post where in it should be only be one, its like it runs twice, so I tested another function using wpdb->query and its also the same its really weird. Please help I dont know what causes and how to fix this issue. Thank you very much. Here is a sample …
Category: Web

Getting error when using wp_insert_post()

When I use wp_insert_post() I get a white page that says "Are you sure you want to do this?". I get no PHP errors. I am using this outside of wordpress and have wp-load.php as well as /wp-admin/includes/file.php included. My code looks like this. require_once $_SERVER['DOCUMENT_ROOT'] . "/wp-load.php"; require_once $_SERVER['DOCUMENT_ROOT'] . "/wp-admin/includes/file.php"; $imagePost = array( 'post_title' => "Test", 'post_content' => 'Content added later.', 'post_status' => 'publish', 'post_author' => 1, 'tags_input' => "'test'", 'post_category' => array(1), 'post_type' => 'post' ); $postID …
Category: Web

Pre insert data when adding new custom post fire a json error

here is what i have tried function mec_locations_cpt_add_content ( $data, $postarr) { //, $postarr if ( 'mec_locations_cpt' !== $postarr['post_type'] ) { return $data; } if ( 'publish' !== $postarr['post_status'] ) { return $data; } $post = get_post( $postarr['ID'] ); //$mec_location_shortcode='[mec-location id=”{LOCATION_ID}” limit_events=”{NUMBER}”]'; //to get from an admin field //$mec_location_shortcode ='<!-- wp:shortcode -->[mec-location id=128]<!-- /wp:shortcode -->'; $data['post_content'] .= '<!-- wp:shortcode -->[mec-location id=128]<!-- /wp:shortcode -->'; $data['post_content'] .= '<!-- wp:paragraph -->#####################################################'.'post='.print_r($post,true).'<!-- /wp:paragraph -->'; $data['post_content'] .= '<!-- wp:paragraph -->@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'.'data='.print_r($data,true).'<!-- /wp:paragraph -->'; $data['post_content'] .= '<!-- …
Category: Web

Add PHP block template to content using wp_insert_post

I have created a block template so that every new post starts with a certain collection of blocks. In the below reduced example, let's say that the block template contains just a heading block and a paragraph block (in reality, the block template is much more complex): function register_custom_block_template() { $post_type_object = get_post_type_object( 'post' ); $post_type_object->template = array( array( 'core/heading', array( 'placeholder' => 'Post Heading', ) ), array( 'core/paragraph', array( 'placeholder' => 'Post Paragraph', ) ), ); } add_action( 'init', …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.