2016-09-28 22 views
1

我想從Wordpress數據庫提取數據,所以我可以把它放到一個新的數據庫中,但有幾個問題算出來。這僅僅是一些Wordpress表格的一個小例子。通過Wordpress數據庫表提取數據

wp_posts 

id | post_type | post_status | 
---------------------------------- 
1 | portfolio | published | ... other data 

wp_postmeta 

id | post_id | meta_key     | meta_value    | 
----------------------------------------------------------------------- 
1 | 1  | item_submission_title | Some title    | ... other data 
2 | 1  | item_technology   | a:1:{i:0;s:4:"2372";} | 
3 | 1  | item_description   | Some description  | 


wp_terms 

id | term_id | name   | 
-------------------------------- 
1 | 2372  | Some name | ... other data 

所以基本上,我必須讓所有的ID從wp_posts其中post_type是組合和post_status是發佈。

使用這些ID我需要得到我的wp_postmeta表中顯示的3個meta_key的meta_value。

item_technology meta_value被序列化,所以我需要然後反序列化它。然後我需要得到它的id(2372)並使用它從wp_terms表中獲得更多的數據。

還有很多我需要做的,但實現上述將讓我瞭解如何最好地做到這一點。我有一種感覺,我可以以某種方式在這裏使用連接,但不確定。目前,我的嘗試是非常無效和不完整的。這是我目前擁有的。

$conn = Database::getInstance(); 

$ids = getIDs($conn); 

$dataArray = array(); 

foreach ($ids as $row) { 
    $data = getData($conn, $row['id']); 
    $dataArray[] = $data; 
} 

function getIDs($conn) { 
    $query = "SELECT `id` FROM `wp_posts` WHERE `post_type` = \"portfolio\" and `post_status` = \"publish\""; 
    $sql = $conn->dbc->prepare($query); 
    $sql->execute(); 
    $row = $sql->fetchAll(); 
    return $row; 
} 

function getData($conn, $id) { 
    $query = "SELECT `meta_value` FROM `wp_postmeta` WHERE `post_id` = $id AND `meta_key` = \"item_submission_title\""; 
    $sql = $conn->dbc->prepare($query); 
    $sql->execute(); 
    $row = $sql->fetchAll(); 
    return $row; 
} 

什麼是實現我所追求的最佳方式?

感謝

回答

1

考慮到你有一個WordPress的數據庫,我假設你也有一個WordPress的安裝連接到它。如果沒有,你應該,爲了確保你以你想要的方式得到你想要的數據,而不必重新創建處理大量WP特定「特性」的代碼。

所以你要做的第一件事就是「bootstrap」WordPress,這樣你就可以在一個東部步驟中獲得數據庫連接和所有的WordPress功能。因此,讓我們假設你創建在WordPress根目錄中的文件...

<?php 

// This includes gives us all the WordPress functionality 
require_once(dirname(__FILE__) . '/wp-load.php'); 

// Set parameters to gather posts 
$args = array(
    'posts_per_page' => -1, 
    'offset'   => 0, 
    'orderby'   => 'date', 
    'order'   => 'DESC', 
    'post_type'  => 'portfolio', 
    'post_status'  => 'publish', 
    'suppress_filters' => true 
    ); 

// Retrieve matching posts 
$posts_array = get_posts($args); 

// Loop through posts to get Meta values 
foreach ($posts_array as $post) 
{ 
    $item_submission_title = get_post_meta($post->ID, 'item_submission_title', true); 
    $item_technology  = maybe_unserialize(get_post_meta($post->ID, 'item_technology', true)); 
    $item_description  = get_post_meta($post->ID, 'item_description', true); 

    // Do something with this information 

    ... 
} 

當然,你現在可能需要得到它到其他數據庫。 WordPress的還可以容納與一個簡單而強大的接口...

$otherDB = new wpdb('username', 'password', 'database', 'localhost'); 

$table = 'other_table'; 

$data = array(
    'item_submission_title' => $item_submission_title, 
    'item_technology'  => $item_technology, 
    'item_description'  => $item_description, 
    ); 

$otherDB->insert($table, $data); 

更多的類WPDB的功能都可以在這裏找到:https://codex.wordpress.org/Class_Reference/wpdb