php - WordPress mysql 和函数

标签 php mysql wordpress

我的目标是将帖子从 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 是图像的 url...

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

祝你好运...

关于php - WordPress mysql 和函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21952388/

相关文章:

php - 重新加载表格而不刷新php中的整个页面

php - 如何在php中通过ldap获取事件目录的密码?

php - 无法用php向mysql输入数据

PHP - 最大总上传大小?

php - mysql比较produst的前两个单词并给出最低价格

css - 如何在 Wordpress 中设置特定页面链接导航栏的样式?

php - 无法从查询 MySQL 数据库中获取结果

php - 无法让用户登录 adm 部分

jquery - Jcarousel无限循环

python - 与 Wordpress 一起部署 Django 应用程序(在 suburl)