• Resolved Pinoss

    (@pinoss)


    Hi,
    I need to insert some posts and pages into wp_post database with Php code directly. Now the code it’s working but I would to insert a Shortcode in the Content of generated post (this shortcode is generated by Shortcode Exec PHP plugin and contains some php lines) . The problem is that when I enter to generated post, the output is : [shortcode]? and not : Hello World (if, for example, the shortocode is echo “Hello World”;

    I’m using this code:

    <?php
    $ip = ‘myserver’;
    $user = ‘mydatabaseuser’;
    $password = ‘mypass’;
    $db_name = ‘mydatabsename’;
    $conn = mysql_pconnect($ip,$user,$password) or die();
    $error = mysql_select_db($db_name,$conn) or die();

    $data=”INSERT INTO mydatabsename.wp_posts (ID, post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_content_filtered, post_parent, guid, menu_order, post_type, post_mime_type, comment_count) VALUES (‘1202’, ‘0’, ‘2014-09-14 00:00:00’, ‘2014-09-14 00:00:00’, ‘[shortcode]’, ‘test’, ‘test’, ‘publish’, ‘open’, ‘open’, ”, ‘test’, ”, ”, ‘2014-09-14 02:06:11’, ‘2014-09-14 05:21:26’, ”, ‘0’, ”, ‘0’, ‘post’, ”, ‘0’);”;

    $result_data=mysql_query($data,$conn);

    echo “inserted!”;

    ?>

    I tried to insert Posts with “wp_insert_post” native function but allways generates duplicates posts in database.

    How Can I solve this issue? Wich method is better to insert posts/pages manually in wp_post database?

    Thanks in advance.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    As a guess, the “Loop” you are using to view the post is not setting up the global $post variable properly. The only other reason you would get a question mark after [shortcode] is if the post author does not appear to have appropriate capabilities.

    The low level post insertion could have something to do with this… or not. At the very least, you should use $wpdb->update() instead of what you are doing, but using wp_insert_post() really is the best way because it is less prone to coding errors. The only reason you would get duplicate posts is if your script runs multiple times. If you are running your insertion script off a filter or action callback, these are often called more than once for any one request even though there doesn’t appear to any reason to do so. You may need a different trigger or prevent your code from being reentered with appropriate logic.

    Also be aware that you cannot specify a post ID in the post array when using wp_insert_post(), you must let the function assign the ID (which is returned by the function on success).

    Do you have issues with built in shortcodes, like [gallery] or only plugin PHP shortcodes? If only plugin, honestly, it’s not that difficult to add additional “built in” style shortcodes instead of relying on a (unsupported) plugin. Your first time code could be a struggle, but afterwards it’s quite straight forward.

    Thread Starter Pinoss

    (@pinoss)

    Hi,
    Thanks for reply.
    I have been testing some code and finally I have one that for the moment, works. This code not duplicate posts/pages and add metas into the database too.

    I created a folder in my server (named test) outside of WordPress folders. Inside this folder I created a PHP file (Test.php) and included core functions in WordPress (wp-load.php).

    I’m using All in One SEO pack , for this reason I wrote some stipulated words in meta variables (like _aioseop_title, _aioseop_description and _aioseop_keywords).

    Here the code:

    <?php
    require_once("../wp-load.php");
    
    global $user_ID;
    $new_post = array(
    'post_title' => 'test_post',
    'post_content' => '[shortcode]',
    'post_status' => 'publish',
    'post_date' => date('Y-m-d H:i:s'),
    'post_author' => $user_ID,
    'post_type' => 'post',
    'tags_input'   => array('tag1,tag2'),
    'post_category' => array(3)
    );
    $post_id = wp_insert_post($new_post); 
    
    $meta_key="_aioseop_title";
    $new_meta_value="title_here";
    
    add_post_meta( $post_id, $meta_key, $new_meta_value, true );
    
    $meta_key="_aioseop_description";
    $new_meta_value="description_here";
    
    add_post_meta( $post_id, $meta_key, $new_meta_value, true );
    
    $meta_key="_aioseop_keywords";
    $new_meta_value="keywords_here";
    
    add_post_meta( $post_id, $meta_key, $new_meta_value, true );
    
    ?>

    Thanks to you bcworkz and Regards to all.

    Thread Starter Pinoss

    (@pinoss)

    If you would like to insert existing tags by ID, change this line:

    'tags_input' => array('tag1,tag2'),

    for this one:

    'tags_input' => array(5,6),

    Regards

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

The topic ‘Insert a Post in Wp_post MYSQL’ is closed to new replies.