I want to manually create posts directly inserting all fields (title, content, etc.) to database. I have read that a function wp_insert_post can do basically all the stuff and you only need to have title, content and category values.
So if I have my variables: $posttitle and $postcontent how do I construct php file to insert those values and create a new post not from admin panel but by loading this php file?
let's say I have:
`<?php
$posttitle = "Title";
$postcontent = "Content";
?>`
What to add into it so that when I run the file it will insert new post into wordpress?
johnh10
Member
Posted 3 years ago #
`<?php
require('wp-blog-header.php');
// Create post object
$my_post = array();
$my_post['post_title'] = "Your title";
$my_post['post_content'] = "Your content";
// Insert the post into the database
wp_insert_post( $my_post );
?>'
elusuario
Member
Posted 2 years ago #
Do i need to include wp-blog-header.php obligatorily?
s_ha_dum (was apljdi)
Member
Posted 2 years ago #
Do i need to include wp-blog-header.php obligatorily?
Not if your .php is already running 'inside' WP as it would be if its a plugin or a template file. Otherwise you do. If you are curious, just try to call wp_insert_post without the require. If you get an error, you know the require is necessary.