Inserting Wordpress Post via External Script - php

I'm trying to insert bulk posts via an external php script:
<?php
require_once '/full/path/to/wp-load.php';
require_once ABSPATH . '/wp-admin/includes/taxonomy.php';
$title = "some post title";
$content = "some content";
$tags= "tag1,tag2,tag3";
$user_id = 1;
// Create post object
$my_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_author' => $user_id,
'post_type' => 'post',
'tags_input' => $tags,
);
$id = wp_insert_post($my_post,true);
?>
$id returns 0 and WP_Error is empty.
Post is inserted to DB with right title and content but without tags. It also fails to use wp_insert_terms() to insert tags or other custom taxonomies.
Did i miss a file to include or is there something i didn't set right to work functions properly?

you don't need to load taxonomy.php.. this functionality is handled by wp-load.php
also i would suggest that you put this on first position in your file:
define( 'WP_USE_THEMES', false );
than you can use wp_insert_post() to add the post.. but to add the tags or category and so on you need to use
wp_set_post_terms()
or
wp_set_object_terms()
with your recieved $id from wp_insert_post.
for more informations check this: https://wordpress.stackexchange.com/questions/18236/attaching-taxonomy-data-to-post-with-wp-insert-post

Please include the wordpress database files first.
require_once 'wp-load.php';
require_once ABSPATH . '/wp-admin/includes/taxonomy.php';
and use the following code to insert post dynamically from the external script.
$newIds = wp_insert_post( array(
'post_title' => $postCSVContent['1'],
'post_content' => $postCSVContent['2'],
'post_type' => 'doors',
'post_status' => 'publish',
'post_author' => 1,
'post_parent' => $parentId
));
Refer the tutorial that explains how to create and update wordpress post programmatically.
http://www.pearlbells.co.uk/insert-udpate-wordpress-post-programmatically/

Related

Why the value of a variable is not entering post_content on just one occasion?

I have a file of autoimport.php
$identif = (string)$numbz;
$myembed = (string)$feed_item->embed;
$feed_item->desc .= "{$identif}{$myembed}<br>{$feed_item->desc}";
$post = array(
'post_author' => '1',
'post_status' => $_POST['status'],
'post_type' => $cpt,
'post_title' => (string)$feed_item->title,
'post_content' => (string)$feed_item->desc,
);
$post_id = wp_insert_post( $post );
update_post_meta($post_id, 'sponsor', (string)$_POST['thelink']);
update_post_meta($post_id, 'contID', (string)$feed_item->id);
There are two variables that I add in post_content in the publish action, always right with manual posting, but when the plugin uses another file to do the import of the post, to trigger the post, this variable $myembed does not enter at all in post_content. Why does it happen?
I make the change in the two files, all right in the manual post.
Importmanual.php has a code almost the same as above, with difference that use post_args:
$post_args = array(
'post_author' => '1',
'post_status' => $status,
'post_type' => $tca,
'post_title' => (string)$ivd['title'],
'post_content' => (string)$ivd['desc'],
);
$post_id = wp_insert_post( $post_args );
And the names of the variables are other, but I copy and paste, there is no writing error. Include the idea is so correct that the code works and the $identif variable comes together perfectly, what could it be?
see them complete: manual post file and auto post file Within the documents search for: ‘hercules’ and you will see a short explanation before the code. https://pastebin.com/s9Xcibd6

Is there a way to programmatically import wordpress pages within wordpress?

I'm trying to create this functionality within my wordpress plugin. Let's say I have a set number of pages that will never change and I want to automatically import them to every wordpress site I set up without having to manually go to the first site, export the xml file containing the pages then import it to the new site. Any thoughts on this?
Thanks
If you know how to loop through your XML file and your XML file is accessible on the other site you could loop through the following code:-
// Create post object
$my_post = array(
'post_title' => wp_strip_all_tags( $post_title ),
'post_content' => $post_content,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => $cat
);
// Insert the post into the database
wp_insert_post( $my_post );
You would need to initiate this code on the install of your plugin.
You can export your pages via default Wordpress export tool. That will result in .xml (WXR) file.
After that you can import the pages via WP-CLI tool on each one site, with the following command:
$ wp import file-name.xml
WXR stands for WordPress eXtended RSS.
You can store the pages in an array and then automatically insert them when your plugin is activated. I'd recommend storing a meta_key for each page that also let's you know it's already been inserted so that you don't create them every time the plugin is activated and deactivated. You can put this in the main file of your plugin. Be sure to replace the numbered pages and slugs with actual page names and to replace "my_plugin" with your plugin's namespace.
<?php
function create_my_plugin_pages() {
$pages = array(
'Page 1' => 'page-1', // Use slugs to create meta-keys
'Page 2' => 'page-2',
'Page 3' => 'page-3'
);
foreach( $pages as $title => $slug ) {
$meta_key = 'my-plugin_'.$slug;
// Check that the page wasn't already created
$existing = get_posts(array(
'post_type' => 'page',
'meta_query' => array(
array(
'key' => $meta_key,
'value' => '1'
)
)
));
// Create page if it doesn't exist
if ( !count($existing) ) {
$new_page = wp_insert_post(array(
'post_title' => $title,
'post_status' => 'publish'
));
add_post_meta($new_page,$meta_key,'1');
}
}
}
register_activation_hook( __FILE__, 'create_my_plugin_pages' );
?>

Wordpress create post with PHP

I want to make a custom PHP script, what can make a post for wordpress.
Here is my code from official page, but its not working:
require("wp-includes/post.php");
// Create post object
$my_post = array(
'post_title' => "mytitle",
'post_content' => "mycontent",
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 1 )
);
wp_insert_post( $my_post );
The error is:
Fatal error: Call to undefined function get_current_user_id() in
/home/MyUser/public_html/wp-includes/post.php on line 2897
What im doing wrong?
The problem is probably that you are not logged in as a backend user. The script is trying to get a backend user id. There has to be a user logged in to create a post. Each post needs a userid, this is used to set the author of the post.
Also are you trying to execute this script inside a plugin or outside wordpress?
For custom PHP script, just add the correct include "wp-load.php" instead of "post.php"
wp_insert_post() - return ID of new post see more
require('wp-load.php');
//require("wp-includes/post.php");
// Create post
$my_post = [
'post_title' => "mytitle",
'post_content' => "mycontent",
'post_status' => 'publish',
'post_author' => 1,
'post_category' => [ 1 ]
];
wp_insert_post( $my_post );

Wordpress add post using front end with different title

I want to create system to insert wordpress post from front-end to logged user. But I have checked same title post many times added by different user.
How I add error message if post title already exist
Here is my code
$u_id = get_current_user_id();
$my_post = array(
'post_title' => 'title',
'post_content' => 'this is post content',
'post_status' => 'publish',
'post_author' => $u_id,
'post_type' => 'customer_post',
);
$post_ID = wp_insert_post( $my_post );
You can prevent this by checking some condition. You should try this. Wrap your code within this condition, it will not allow post with same title.
if (!get_page_by_title($title, 'OBJECT', 'post') ){
//your code goes here.
}

Wordpress Create post programmatically then show post on index.php

I have been trying this project of mine for a while now and I have got no luck. I'm trying to create 8 post programmatically in functions.php. I need these to only post 1 time. The problem I've been having is everytime I refresh the page the post will just auto create more. Here is my code to create post progammatically in functions.php.
<?php // Create post object
$my_post = array(
'post_title' => 'How to make your diet success',
'post_name' => '7-ways-to-make-succes-Diet',
'post_content' => 'my content',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39)
);
// Insert the post into the database
wp_insert_post( $my_post ); ?>
The only proble with this code is that it auto creates more post every time the page refreshes. I'm going to be creating 8 of these functions and I only want them to be created once. A code example would be great.
Next, I want to display the post on my index.php. I want to get these post individually. Here is my code I have so far.
<div class="post1"><?php $post_id = wp_insert_post( $post, $wp_error );
//now you can use $post_id withing add_post_meta or update_post_meta ?> </div>
<div class="post2"><?php $post_id = wp_insert_post( $post, $wp_error );
//now you can use $post_id withing add_post_meta or update_post_meta ?> </div>
I'm pretty sure I need to either call the slugs or post name to get them individually. Yes, I have tried this method as well as 10 other methods but nothing has worked. The closest I got was it to display the post name. Code examples would be great. I will be so grateful and probably donate some money via paypal if someone can get this working for me. Thanks.
functions.php is not a good place for programatically create pages nor posts. You should create a plugin (it is simple as creating custom theme) and create posts in its activation function. This function is called only on your plugin activation. Read also about plugin deactivation a uninstall hooks
The reason why your posts get created again and again is that file functions.php is called each time the page is requested. If you insist on creating posts in functions.php, you should wrap your wp_insert_post by a condition chcecking whether your posts are already created - than get_posts function would suit your needs.
<?php
//Use either post slug (post_name)
$post = get_posts( array( 'name' => '7-ways-to-make-success-diet' ) );
/*or $post = get_posts( array( 'name' => sanitize_title('My Single.php Test') ) );
if you do not set the post_name attribute and let WordPress to set it up for you */
if ( empty($post) ) {
// Create post object
$my_post = array( 'post_title' => 'My Single.php Test', 'post_name' => '7-ways-to-make-success-diet', 'post_content' => 'my content4654654', 'post_status' => 'publish', 'post_author' => 1, 'post_category' => array(8,39) );
// Insert the post into the database
wp_insert_post( $my_post );
}
?>
Also, get_posts will help you to bring your posts on front page. Eg.
<?php
$post = get_posts( array( 'name' => 'How to make your diet success' ) );
echo $post->post_title;
...
?>

Categories