• Resolved szunyi

    (@szunyi)


    Hello,

    How can i insert a post from a PHP file.
    Please give me a working “wp_insert_post” example.

    Thanks
    bye

Viewing 15 replies - 1 through 15 (of 16 total)
  • // Create post object
    $my_post = array(
      'post_title'    => wp_strip_all_tags( $_POST['post_title'] ),
      'post_content'  => $_POST['post_content'],
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_category' => array( 8,39 )
    );
    
    // Insert the post into the database
    wp_insert_post( $my_post );

    from wp_insert_post

    Thread Starter szunyi

    (@szunyi)

    but first need some include, right?
    if yes what kind of files need to include?

    I don’t see what includes you are talking about and I would need much more info to understand your problem.
    what are you going to use the insert for?
    do you have a lot of posts that you are trying to bulk insert once or is it some kind of functionality that fires after a certain event?
    I need to understand your need to be able to answer you accordinglly.

    Thread Starter szunyi

    (@szunyi)

    Okay, i want to insert post to wordpress from PHP file.
    When i open the “mydomain.com/testinsert.php” that script will post the content to db and the wordpress will show it…

    ok, try adding this to your php file:

    require("wp-includes/post.php");

    Thread Starter szunyi

    (@szunyi)

    <?php

    ini_set(‘display_errors’, 1);
    error_reporting(E_ALL);

    require(“wp-includes/post.php”);

    // Create post object
    $my_post = array(
    ‘post_title’ => “qwe”,
    ‘post_content’ => wp_strip_all_tags($content),
    ‘post_status’ => ‘publish’,
    ‘post_author’ => ‘1’,
    ‘post_category’ => array( 1,2 )
    );

    wp_insert_post( $my_post );

    ?>

    If your question has been answered, we would love if you would mark this topic as resolved in the right hand sidebar. This helps our volunteers find the topics that still need attention and more people will get helped, possibly like you did.

    Thread Starter szunyi

    (@szunyi)

    still not answered, i dont know still how can i insert post to my wordpress πŸ™

    well you just copied the code and put it here so I assumed you found the solution.
    did you try this code? what happened if you did?

    Thread Starter szunyi

    (@szunyi)

    yes i tried, but the error is:
    “Fatal error: Call to undefined function wp_strip_all_tags() in /home/penzker/public_html/test.php on line 11”

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Wait, you’re trying to use WordPress outside of WordPress?

    Thread Starter szunyi

    (@szunyi)

    okay, start again, my question is how can i insert post to my wordpress from PHP file?

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    What do you mean by “post”?

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    We have no knowledge of your system that you’re running outside of WordPress. If your external system creates posts then you need to tell us how it does it.

    Thread Starter szunyi

    (@szunyi)

    ok thx

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Insert post from PHP script’ is closed to new replies.