0

My aim is to import posts from a node.js server to the wordpress database, thus is due to the large amount of records it will be trawling daily..

Do you know of any cheat sheets that show the mysql that a wordpress function uses?

For example

wp_insert_post()

//The above function uses this sql

INSERT INTO wp_posts VALUES(field1) etc etc

Thanks in advance

1 Answer 1

2

Auto generating content... Hmmm lets try ..

<?php
//First you need the required files...
include('../wp-load.php' );
require_once('../wp-admin/includes/media.php');
require_once('../wp-admin/includes/file.php');
require_once('../wp-admin/includes/taxonomy.php');
require_once('../wp-includes/taxonomy.php');

// Flush the wordpress db...
$wpdb->flush();

// Get your data from any feed. and lets say you will process it in a foreach loop..

foreach($somedata as $key => $value){

   // i don't know your data structure so i give a symbolic example..
   $post = array(  
      'post_author'    => 2,
      'post_content'   => $post_content,
      'post_excerpt'   => $post_excerpt,
      'post_status'    => 'publish',
      'post_title'     => $post_title, // aware & ' so something to escape...
      'post_type'      => 'post',
      'post_date'      => $pub_date, // Y-m-d H:i:s
      'tags_input'     => $tags // comma separated tags..
    );  

    $post_id = wp_insert_post( $post, $wp_error );


        wp_set_object_terms( $post_id, $cat_ids, 'category' ); // $cat_ids is an array!

     }

?>

The code above will add posts for you.. running it via cron and setting parameters to avoid duplicates your job..

Well if you need to import a remote image and add it to the post as featured you need a function like this..

function somatic_attach_external_image( $url = null, $post_id = null, $thumb = null, $filename = null, $post_data = array(),$resimdesc=null ) {
    if ( !$url || !$post_id ) return new WP_Error('missing', "Need a valid URL and post ID...");
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
    // Download file to temp location, returns full server path to temp file, ex; /home/user/public_html/mysite/wp-content/26192277_640.tmp
    $tmp = download_url( $url );

    // If error storing temporarily, unlink
    if ( is_wp_error( $tmp ) ) {
        @unlink($file_array['tmp_name']);   // clean up
        $file_array['tmp_name'] = '';
        return $tmp; // output wp_error
    }

    preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $url, $matches);    // fix file filename for query strings
    $url_filename = basename($matches[0]);                                                  // extract filename from url for title
    $url_type = wp_check_filetype($url_filename);                                           // determine file type (ext and mime/type)

    // override filename if given, reconstruct server path
    if ( !empty( $filename ) ) {
        $filename = sanitize_file_name($filename);
        $tmppath = pathinfo( $tmp );                                                        // extract path parts
        $new = $tmppath['dirname'] . "/". $filename . "." . $tmppath['extension'];          // build new path
        rename($tmp, $new);                                                                 // renames temp file on server
        $tmp = $new;                                                                        // push new filename (in path) to be used in file array later
    }

    // assemble file data (should be built like $_FILES since wp_handle_sideload() will be using)
    $file_array['tmp_name'] = $tmp;                                                         // full server path to temp file

    if ( !empty( $filename ) ) {
        $file_array['name'] = $filename . "." . $url_type['ext'];                           // user given filename for title, add original URL extension
    } else {
        $file_array['name'] = $url_filename;                                                // just use original URL filename
    }

    // set additional wp_posts columns
    if ($resimdesc) {
        $post_data['post_title'] = $resimdesc;         // just use the original filename (no extension)
    }

    // make sure gets tied to parent
    if ( empty( $post_data['post_parent'] ) ) {
        $post_data['post_parent'] = $post_id;
    }

    // required libraries for media_handle_sideload
    require_once(ABSPATH . 'wp-admin/includes/file.php');
    require_once(ABSPATH . 'wp-admin/includes/media.php');
    require_once(ABSPATH . 'wp-admin/includes/image.php');

    // do the validation and storage stuff
    $att_id = media_handle_sideload( $file_array, $post_id, null, $post_data );             // $post_data can override the items saved to wp_posts table, like post_mime_type, guid, post_parent, post_title, post_content, post_status

    // If error storing permanently, unlink
    if ( is_wp_error($att_id) ) {
        @unlink($file_array['tmp_name']);   // clean up
        return $att_id; // output wp_error
    }

    // set as post thumbnail if desired
    if ($thumb) {
        set_post_thumbnail($post_id, $att_id);
    }

    return $att_id;
}

You will use this function inside the foreach.. $imgsrc is the url of the image...

$attach_id = somatic_attach_external_image( $imgsrc, $post_id, $thumb = null, $filename = null, $post, $title );
add_post_meta($post_id, '_thumbnail_id', $attach_id, true);

Good luck...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.