计算每个类别的所有特殊自定义字段(Wordpress)

时间:2015-12-10 09:19:57

标签: php wordpress

使用此脚本,我的特殊自定义字段“昵称”仅计算一次,如果这些字段在帖子中可用两次或更多次。

有没有办法计算帖子中名为“昵称”的所有特殊自定义字段?

这是剧本:

<?php
$posts_with_nickname = get_posts(array(
        'category__in' => array(2,5),
        'meta_key' => 'Nickname',
        'showposts' => -1,
));

printf('I have %d posts with "Nickname"!', count($posts_with_nickname));
?>

1 个答案:

答案 0 :(得分:0)

您可能想要使用meta_query

<?php
$posts_with_nickname = get_posts(
    'category__in' => array(2,5),
    'meta_query'   => array(
        array(
            'key'     => 'Nickname',
            'value'   => '',
            'compare' => 'NOT LIKE',
        ),
    ),
    'showposts'    => -1,
);

printf('I have %d posts with "Nickname"!', count($posts_with_nickname));