• 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?

Viewing 3 replies - 1 through 3 (of 3 total)
  • `<?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 );
    ?>’

    Do i need to include wp-blog-header.php obligatorily?

    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.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘How to manually create posts with wp_insert_post function?’ is closed to new replies.