• Resolved harjeet singh

    (@harjeet-singh)


    I have one custom page in my blog which shows posts from selected categories. I want to show the post count for the selected categories besides the link for custom page .
    i.e Custom Page (100)
    I searched and got the code for all post count.

    <?php
    $count_posts = wp_count_posts();
    $published_posts = $count_posts->publish;
    echo $published_posts;
    ?>

    what i want to achieve is . . .

    custom result = all posts count – (excluded cat1 post count + excluded cat2 post count)

    I think it clears my question ..

Viewing 3 replies - 1 through 3 (of 3 total)
  • Didn’t test this, but something like:

    $args=array(
      'category__not_in' => array(1,2),
      'post_type' => 'post',
      'post_status' => 'publish',
      'showposts' => -1,
      'caller_get_posts'=> 1
    );
    $poststocount=get_posts($args);
    echo 'all posts count '. count($poststocount);
    Thread Starter harjeet singh

    (@harjeet-singh)

    Thanks MichaelH , thats works perfectly fine. There’s new problem for the values for the excluded categories . I am pulling the values from database and using it as follows but it does not work.

    $custom_post_excluded_categories = explode(‘,’,get_option(‘excluded_cats’));
    $args=array(
    ‘category__not_in’ => array($custom_post_excluded_categories),
    ‘post_type’ => ‘post’,
    ‘post_status’ => ‘publish’,
    ‘showposts’ => -1,
    ‘caller_get_posts’=> 1
    );
    $poststocount=get_posts($args);
    echo ‘all posts count ‘. count($poststocount);

    the value for excluded_cats = 21,13
    Using the explode command there is no subtraction in the post count.But if i use

    $args=array(
    ‘category__not_in’ => array($excluded_cats),
    ‘post_type’ => ‘post’,
    ‘post_status’ => ‘publish’,
    ‘showposts’ => -1,
    ‘caller_get_posts’=> 1
    );
    $poststocount=get_posts($args);
    echo ‘all posts count ‘. count($poststocount);

    In the result there is subtraction but only for the first category posts.

    Thread Starter harjeet singh

    (@harjeet-singh)

    Its solved MichaelH thanks for the support. Here is the final code.

    $args=array(
    ‘category__not_in’ => explode(‘,’,get_option(‘excluded_cats’)),
    ‘post_type’ => ‘post’,
    ‘post_status’ => ‘publish’,
    ‘showposts’ => -1,
    ‘caller_get_posts’=> 1
    );
    $poststocount=get_posts($args);
    echo ‘all posts count ‘. count($poststocount);

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom Post Count ! Plz Help !’ is closed to new replies.