2014-11-08 102 views
-1

我一直在拉我的頭髮,試圖找出如何做到這一點。我創建了一個名爲cp_country的自定義字段。現在Mysql中的每條記錄都有一個值爲cp_country。問題是我不明白Wordpress如何將數據索引到MySql中。在wordpress中從Mysql表中獲取數據

例如看看這裏

enter image description here

通常情況下,我會被用來提取行一樣SELECT * FROM table WHERE cp_country='United Kingdom' 但正如你所看到的結果並不行,則屬性由每行上市加入了一個帖子ID。

我如何得到結果WHERE cp_country='United Kingdom'?例如,一組結果每個都有cp_country,cp_street,cp_price

+0

想要在帖子中使用此信息嗎? – rnevius 2014-11-08 13:57:59

+0

不在帖子中。這將以兒童爲主題。我想檢索所有職位在哪裏'cp_country =英國' – user892134 2014-11-08 14:01:37

+0

我的答案缺少一些東西?它對你有幫助嗎? – rnevius 2014-11-08 14:55:50

回答

0

你可以做一個WordPress查詢...假設你想要在前端使用。例如,要查詢與'cp_country' == 'United Kingdom'所有帖子:

<?php 
// Query Args 
$args = array(
    'meta_key' => 'cp_country', 
    'meta_value' => 'United Kingdom' 
); 

// The Query 
$the_query = new WP_Query($args); 

// The Loop 
if ($the_query->have_posts()) { 
    while ($the_query->have_posts()) { 
     $the_query->the_post(); 
     // Do things with the post data 
    } 
} else { 
    // no posts found 
} 
/* Restore original Post Data */ 
wp_reset_postdata(); 

您也可以使用get_posts(),採用了類似的過程。 get_posts()將根據一組參數返回一個帖子數組。