WordPress的MySQL和功能

妮基·尤(NickyYo)

我的目标是将帖子从node.js服务器导入到wordpress数据库中,因此由于每天都会拖曳大量记录。

您是否知道显示wordpress函数使用的mysql的任何备忘单?

例如

wp_insert_post()

//The above function uses this sql

INSERT INTO wp_posts VALUES(field1) etc etc

提前致谢

阿巴他奇

自动生成内容...嗯,可以尝试..

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

     }

?>

上面的代码将为您添加帖子。.通过cron运行它并设置参数以避免重复您的工作。

好吧,如果您需要导入远程图像并将其添加到帖子中,则需要这样的功能。

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;
}

您将在foreach内部使用此函数。$ imgsrc是图像的网址...

$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);

祝你好运...

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章