With this script my special custom field "Nickname" is counted only once, also if these field is available twice or more in a post.
Is there a way to count all special custom fields called "Nickname" in a post?
Thats the script:
<?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));
?>
You may want to use 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));