0

How can i get the count for published post from my custom post type , from my taxonomy.

I have Custom Post Type : trailers, Taxonomy : trailere-noi

<?php 

$items = get_posts( array(
    'post_type'   => 'trailers',
    'numberposts' => -1,
    'taxonomy'    => 'trailere-noi',
    ));

$count =  count( $posts );
echo $count;


?>

I use this code it conunt for how posts are display.. how can get the count for all posts in that taxonomy?


Response:

<?php 
$related_posts = get_field( 'subtitrare_film' );
$count =  count( $related_posts );
echo $count;
?>

Subtitrare_film - is the relathionship field (INFO :http://www.advancedcustomfields.com/docs/field-types/relationship/) This code Retrive me the field counting number, but the problem is: if there is nothing atached to that Custom Field ( Relationship fild) it retrive me number 1 , i want to display 0 ( if there is nothing atachhed.)

  • ... you need to be counting $items not $posts. $count = count($items); – s_ha_dum Jan 12 '13 at 15:47
2

try something like this:

$items = get_posts( array(
    'post_type'   => 'trailers',
    'numberposts' => -1,
    'taxonomy'    => 'trailere-noi',
    ));

echo $posts->post_count;
  • nope, is not retriving... – Batman Jan 10 '13 at 22:24
  • Please explain why that could solve the problem. – fuxia Jan 11 '13 at 0:31
0

i see another thing in your code, your count() function is counting something that does't exists, the variable $posts, so please try with $items->post_count;

another option is :

global $wp_query
$items = get_posts( array(
    'post_type'   => 'trailers',
    'numberposts' => -1,
    'taxonomy'    => 'trailere-noi',
    ));

echo $wp_query->found_posts

$wp_query should return the last query and since get_posts() works with wp_query this solution should work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.