数个帖子有特定的元键或值?

时间:2019-02-28 20:21:03

标签: wordpress plugins

我要实现的是显示具有特定元键或值的帖子数。我正在获取帖子以及元键和值的列表,但不知道如何显示它们。我正在使用可重复字段存储数据。正确存储工作。 例如,现在,我在两个帖子中都有“年龄”元数据,所以我如何不算年龄的帖子。年龄=第2个职位的人数。

我的代码:

global $wpdb;
$query = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}postmeta WHERE (meta_key = 'repeatable_fields') ");
$array = wp_json_encode($query);
print_r($array);

Outout:

  

[{“ meta_id”:“ 312”,“ post_id”:“ 108”,“ meta_key”:“可重复字段”,“ meta_value”:“ a:2:{i:0; a:2:{s: 4:\“名称\”; s:6:\“ Zaheer \”; s:5:\“电话\”; s:3:\“ 123 \”;} i:1; a:2:{s: 4:\“名称\”; s:6:\“贤者\”; s:5:\“电话\”; s:11:\“ 09190219218 \”;}}“},{” meta_id“:” 323 “,” post_id“:” 121“,” meta_key“:”可重复字段“,” meta_value“:” a:2:{i:0; a:2:{s:9:\“ iif_label \”; s:4 :\“城市\”; s:11:\“ iif_details \”; s:7:\“卡拉奇”;} i:1; a:2:{s:9:\“ iif_label \”; s:3 :\“年龄\”; s:11:\“ iif_details \”; s:2:\“ 12 \”;}}“},{” meta_id“:” 329“,” post_id“:” 126“,” meta_key“:” repeatable_fields“,” meta_value“:” a:1:{i:0; a:2:{s:9:\“ iif_label \”; s:3:\“ Age \”; s:11: \“ iif_details \”; s:2:\“ 12 \”;}}“},{” meta_id“:” 332“,” post_id“:” 128“,” meta_key“:” repeatable_fields“,” meta_value“: “ a:3:{i:0; a:2:{s:9:\” iif_label \“; s:7:\” Country \“; s:11:\” iif_details \“; s:8:\ “巴基斯坦\”;} i:1; a:2:{s:9:\“ iif_label \”; s:4:\“城市\”; s:11:\“ iif_details \”; s:9:\ “ Islamabad \”;} i:2; a:2:{s:9:\“ iif_label \”; s:3:\“ Age \”; s:11:\“ iif_details \”; s:2:\ “ 12 \”;}}“}]

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

$count_age = $wpdb->get_col( $wpdb->prepare(
    "
    SELECT      count(meta_id)
    FROM        {$wpdb->prefix}postmeta
    WHERE       meta_value LIKE '%%%s%%'
    ",
    'Age'
));

此处有关get_col()的更多信息:https://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Column