3

Is it possible to calculate the percentage of posts in each given category? I'd like to make a bar chart to visualize the amout of posts in those categories.

I thought what one needs would be the total amount of posts, the amount of posts in each category and then a PHP function that calculates percentages with those values and applies a variable to it which I need to use when building the bar chart.

I am not very familiar with extended PHP, so if anyone could show me the right direction, I'd be grateful

3

With a little CSS, this will create a list of categories like so:

enter image description here

Basic CSS:

<style>
  .bar { height:4px; background:blue; }
</style>

The PHP:

<?php   
    // To customize visit http://codex.wordpress.org/Function_Reference/get_categories
    $categories = get_categories();
    // Get total posts, this allows for posts to have more than one category and accommodates for custom $args in get_categories($args)
    $total_posts = 0;
    foreach ($categories as $category) {
        $total_posts += $category->count;
    }

    // Loop through the categories
    $total_check = 0;
    foreach ($categories as $category) {
        // Get the % and round to 2 decimal places
        $percentage = round( (($category->count / $total_posts)*100), 2 );
        // Just checking to see if they will add up to 100 at the end
        $total_check += $percentage;
        echo '
            <div class="category-wrap">
                <h3>'.$category->cat_name.'</h3>
                <div class="bar" style="width:'.$percentage.'%;"></div>
            </div><!-- .category-wrap -->
        ';
    }
    // Just checking to see that they all add up to 100, delete or comment out afterward
    echo $total_check;
?>
| improve this answer | |
  • Please add a proper explanation to what your code doe and how it works. Here we go just don't cut it. Please file an edit and add all relevant info – Pieter Goosen Jul 16 '14 at 18:04
  • Appreciate the down vote, 6 hours in without a single response. My bad :\. @PieterGoosen Ok, I've had a chance to edit my question. – deflime Jul 16 '14 at 18:42
  • That down vote wasn't from me though:-) – Pieter Goosen Jul 16 '14 at 18:44
  • Figured as much. Was only just able to find a moment to update my answer, didn't think you were that closely watching over my post with frustration. :) – deflime Jul 16 '14 at 18:45
  • Just one thing, there are some bugs in your code. Notice: Undefined variable: total_posts and Undefined variable: total_check – Pieter Goosen Jul 16 '14 at 18:48
0

Depending on where you are wanting to use this, you could find different ways to deal with it, but I've provided code for a simple function you can put in your functions.php and call anywhere in your theme to get an array of the percentages.

This method makes use of a combination of wp_count_posts() and get_categories().

You'll probably want to do some rounding when you convert the decimals to percentages. I left that out so that you could make the decision on how you wanted to deal with the numbers.

//function to return array of percentage of posts in each category
function wpse_154771() {
    //get # of posts of each status
    $total = wp_count_posts();
    //# of published posts
    $total_posts = $total->publish;

    $args = array();

    //if you want to include categories with no posts uncomment the line below
    //$args['hide_empty'] = 0;

    //get all categories
    $categories = get_categories($args);

    //array for storing category percentage
    $percent_array = array();

    //iterate through categories and get percentage of posts
    foreach($categories as $cat) {
        //get the decimal representation of this percentage, you can convert as needed
        $cat_percentage = $cat->count / $total_posts;

        //store percentage in array with category slug as array index
        $percent_array[$cat->slug] = $cat_percentage;
    }

    //return our array of percentages
    return $percent_array;
}

Use the function like this: $percentages = wpse_154771();

Then you can iterate through that array and do what you'd like with it. There will be many ways you can approach generating a bar graph so this answer focuses on the title of the question and remains agnostic to your desired graph approach.

| improve this answer | |

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.