Wordpress create post with PHP - 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 );

Related

How to create a wordpress page automatically when a subscriber is registered to wordpress

Further i need the automatically created page to be edited by the respective subscribe only and when published it should be visible to all users who visit the website
Perhaps you need to use the user_register hook and execute the function in which you create the post, and to create a post, you need to use wp_insert_post function
add_action('user_register','my_function');
function my_function($user_id){
// Create post object
$my_post = array(
'post_title' => 'Title of page',
'post_type' => 'post', // Set type of your post
'post_content' => $_POST['post_content'],
'post_status' => 'publish', // Page published
'post_author' => $user_id // Assign page author
);
// Insert the post into the database
wp_insert_post( $my_post );
}
You can also manage Roles and Capabilities for your post type
Examples:
https://wordpress.stackexchange.com/a/208739/193674
https://wordpress.stackexchange.com/a/108375/193674

Create a 2 different Posts in 2 different custom post type after function

I am trying to create a 2 different posts in 2 different post types. What i am trying to achieve is to have 2 unique posts, and if a button is clicked twice, it shouldn't create the post again.
<?
$custom_title = $company_name . '-' . $formindex;
$args = array(
'post_type' => 'dogovori',
'post_title' => $custom_title,
'post_name' => $company_name,
'post_status' => 'publish',
'post_content' => $content_dogovor,
);
$post_id = post_exists($custom_title) or wp_insert_post($args);
$args1 = array(
'post_type' => 'svidetelstva',
'post_title' => $custom_title,
'post_name' => $company_name,
'post_status' => 'publish',
'post_content' => $content_dogovor,
);
$post_id1 = post_exists($custom_title) or wp_insert_post($args1);
This is what i am using as a code. It works perfectly and creates the post only in 'dogovori'. What i want is to create at the same time in 'svidetelstva' as well.
The function post_exists does not pay any attention to your post type. So first you are inserting a new post with a custom title. When you check if a post exists the second time, it finds the first post you inserted.
Please reference this SA answer to check if a specific custom post exists with a title:
https://wordpress.stackexchange.com/a/300151

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

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

how to assign page template dynamically in wordpress

I have a WordPress page created dynamically
$my_post = array(
'post_title' => 'page-for-download',
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page'
);
now my question is how to assign page template to this page dynamically
WordPress keeps the page template in a post meta entry(named _wp_page_template). Here is what you should do once you create the page:
update_post_meta( $new_post_id, '_wp_page_template', 'custom-template.php' );
Where $new_post_id is the result of wp_insert_post()(I assume that this is what you are using to create the new post). Note, that you might want to check to see if you have an actual id(by default wp_insert_post() will return false if it fails to create a new post).
You can see that information in the first NOTE in the Parameters section of the WordPress codex page Function Reference/wp insert post

Categories