0

I'm trying to count the number of sticky_posts inside a specific category.

first I get all the sticky posts :

$sticky = get_option( 'sticky_posts' );

then here are my args :

$args_sticky = array(
'post_type' => 'post',
'post__in'  => $sticky,
'post__not_in'  => array($post->ID),
'category__in' => wp_get_post_categories($post->ID),
);

and then I try to count the number of posts from those args :

$sticky_number = count($args_sticky);

the problem is that I get not the right result. I don't know what I'm doing wrong...

here is my full php code :

<?php 
$sticky = get_option( 'sticky_posts' );
$args_sticky = array(
'post_type' => 'post',
'post__in'  => $sticky,
'post__not_in'  => array($post->ID),
'category__in' => wp_get_post_categories($post->ID),
);


$sticky_number = count($args_sticky); 

echo $sticky_number;

?>

can anybody help me with this ?

thanks a lot for your help,

  • 1
    Where's your WP_Query call? Currently you're just counting your arguments! – birgire Sep 26 '16 at 15:56
  • Hum yes I know.... how can I count using the wp-query ? – user2882154 Sep 26 '16 at 16:08
  • @birgire I'm trying to count the sticky posts from the posts defined in my args. and store the result in a variable. – user2882154 Sep 26 '16 at 16:10
  • Did you try the found_posts attribute of WP_Query ? – birgire Sep 26 '16 at 16:56

Your Answer

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

Browse other questions tagged or ask your own question.