Support » Fixing WordPress » using the wp_insert_post command

  • I have an administration application on mny deal site, when people add a coupon, we automatically create a post in our forums form the same form. I was looking to do the same to my wordpress blog. At first I tried just inserting into the DB, but then I found what looks to be a very useful function wp_insert_post, but after including the post.php, functions.php and plugin.php so all of the called functions have their neccesary functions. It led me to include cache.php .

    Now I’m getting an error saying that I performed get_ on a non object. I’m new to using object in php so I figure there must be an easy way for me to call this function from my other programs that I must be missing…

    the file I am trying to use the wp_insert_post function in is outside of the standard wp directory tree… I’m not sure if that’s an issue or not.

    can anyone point me in the right direction?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi, yeah I wanted to do pretty much the same thing and I had pretty much the same problem. Just like you I made the error to start by including files one by one waiting for the server to tell me what were the missing dependencies… Here’s how I got it solve :

    1. Remove all include and require statements and replace it with this single statement :

    require_once(‘yourwordpressdirectory/wp-config.php’);

    this is the only file you really need to include to have a working wp_insert_post function… actually here’s a whole file you can test :

    <?php
    /*
    Plugin Name: ImportWikiNews
    Plugin URI: http://www.emuwiki.com
    Description: A brief description of the plugin.
    Version: The plugin’s Version Number, e.g.: 1.0
    Author: Name Of The Plugin Author
    Author URI: http://URI_Of_The_Plugin_Author
    */

    require_once(‘yourwordpressdirectory/wp-config.php’);

    function post_a_wiki_news() {

    // create post object
    class wm_mypost {
    var $post_title;
    var $post_content;
    var $post_status;
    var $post_author; /* author user id (optional) */
    var $post_name; /* slug (optional) */
    var $post_type; /* ‘page’ or ‘post’ (optional, defaults to ‘post’) */
    var $comment_status; /* open or closed for commenting (optional) */
    }

    // initialize post object
    $wm_mypost = new wm_mypost();

    // fill object
    $wm_mypost->post_title = rand(1,1000);
    $wm_mypost->post_content = ‘test’;
    $wm_mypost->post_status = ‘publish’;
    $wm_mypost->post_author = 1;
    $wp_rewrite->feeds = ‘no’;

    // Optional; uncomment as needed
    // $wm_mypost->post_type = ‘page’;
    // $wm_mypost->comment_status = ‘closed’;

    // feed object to wp_insert_post
    wp_insert_post($wm_mypost);

    }

    ?>

    ———————

    This file actually works for me. the way its made now, it creates a new post with a random number between 0 and 1000, but you can modify this for whatever you need. It needs to be put in /wordpress/wp-content/plugins/ and activated in the wordpress plugin panels (*not sure that’s necessary but just to be sure). Then since it defines a function post_a_wiki_news(), you can just put another php file elsewhere on your server that would contain this :

    <?php

    include_once(‘yourwordpressdirectory/wp-content/plugins/testwp.php’);

    post_a_wiki_news();

    ?>

    and everytime you browse to this file on your browser, there is a new post that is created that contains a random number as a title (you will replace this with whatever you need

    Jj23 – Thanks for the code. Very useful. Do you know by any chance where I can fin a complete list of parameters for the wp_insert_post function?

    I looked on this page http://codex.wordpress.org/Function_Reference/wp_insert_post I noticed that your code has extra parameters not listed there like “feeds”.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘using the wp_insert_post command’ is closed to new replies.