2014-11-08 103 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()将根据一组参数返回一个帖子数组。