WordPress.org

Ready to get started?Download WordPress

Forums

Anyone interested in a post count function? (16 posts)

  1. Clint
    Member
    Posted 9 years ago #

    Hi. I keep an unpublished weblog as a work diary.
    I couldn't find a WordPress function that provided a convenient post count function with the options I wanted, so I wrote one.
    It gives posts of selected category and/or posts over recent time span. For example, post_count('surfing', '10) gives the number of posts to the surfing category over the past ten days.
    Is anyone interested in such a function? If so, is there a place that I could post it without it being buried?
    Thanks,
    -Clint

  2. Mark (podz)
    Support Maven
    Posted 9 years ago #

    Post it here, and to the Wiki, get in touch with LL and get it posted at http://www.weblogtoolscollection.com/, and then Carthik at http://wordlog.com/ and then whoever is behind http://bloggingpro.com/
    No danger of burial after that :)
    Cool idea for a plugin too.

  3. Clint
    Member
    Posted 9 years ago #

    Sure thing. I'm contacting Carthik, but I couldn't find any contact information at the other two sites.
    To install:

    • Cut and paste the following code into a local .php file (I chose countposts.php)
    • Put this php file in your wp-content/plugins directory
    • Activate plugin
    • In your index.php, or wherever you want to use it, call the function like this: <?php count_posts('daily log', '60'); ?>. This yields the number of posts over the last sixty days in the 'daily log' category.

    Enjoy!
    ----

    <?php
    /*
    Plugin Name: count_posts
    Description: A simple function to count posts, with the option of filtering by category or over a recent time span (in days).
    Version: 1.0
    Author: Clint Howarth
    */
    function count_posts ($category = '',
    $span = '0')
    {
    global $tableposts, $tablecategories, $tablepost2cat, $wpdb;
    $now = current_time('mysql');
    # want the date of $span days ago
    if ($span != 0) {
    $then = gmdate('Y-m-d H:i:s',
    (time() + (get_settings('gmt_offset') * 3600) -
    ($span * 86400))
    );
    }
    # get category id based on name
    if (!empty($category)) {
    $catid = $wpdb->get_var("SELECT cat_id FROM $tablecategories
    WHERE cat_name = 'daily log' ");
    }
    # start the query
    $query = "SELECT COUNT(*) FROM $tableposts ";
    if (!empty($category)) {
    $query .= "LEFT JOIN $tablepost2cat ON
    ($tableposts.ID = $tablepost2cat.post_id) ";
    }
    $query .= "WHERE (post_date <= '$now') ";
    if (!empty($category)) {
    $query .= "AND (category_id = $catid) ";
    }
    if ($span != 0) {
    $query .= "AND (post_date >= '$then') ";
    }
    $query .= "AND (post_status = 'publish') ";
    $number = $wpdb->get_var($query);
    echo $number;
    }
    ?>

  4. Clint
    Member
    Posted 9 years ago #

    By the way, this function also works without a category selected, or without a time span selected. :)

  5. Anonymous
    Unregistered
    Posted 9 years ago #

    xtra cool. I think this is pretty useful and it looks great on my blog.
    thank you, man.

  6. tedfox
    Member
    Posted 9 years ago #

    Very neat if you require all the flexibility....
    For a plugin with less flexibility... and other stuff.. u can check out
    http://mtdewvirus.com/wp-hacks/
    too

  7. hanni
    Volunteer Moderator
    Posted 9 years ago #

    Great code, thanks :)

  8. carthik
    Member
    Posted 9 years ago #

    Saved for posterity at Wordlog.com.
    (might be a while before the post appears. I posted to the future :) )
    The plugin itself is available at
    http://www.carthik.net/wpplugins/count_posts.phps

  9. mfischer2
    Member
    Posted 9 years ago #

    Clint & Carthik,
    A minor correction in your code. You have hardcoded "daily_log" into the category name.
    You need to change this portion (4th line down):
    # get category id based on name
    if (!empty($category)) {
    $catid = $wpdb->get_var("SELECT cat_id FROM $tablecategories
    WHERE cat_name = 'daily log' ");
    }
    To This:
    WHERE cat_name = '$category' ");

  10. carthik
    Member
    Posted 9 years ago #

    I have fixed this in the source I made available for the plugin :
    http://www.carthik.net/wpplugins/count_posts.phps
    Thanks!

  11. benwisdom
    Member
    Posted 8 years ago #

    This is a great plugin. But I was wondering: is there a way that it can be modified so that it doesn't "echo" the output number of posts asked for, but allows it to be used in a mathematical or logical operation?

    Something like

    <?php $var = count_posts('daily log', '60'); ?>

    or

    <?php if (count_posts('daily log', '60')==0) { ... }; ?>

    I've tried to use it this way, and it doesn't seem to work:

    http://www.sevenrealms.org/?page_id=377

    Here's where I'm currently using it as it is:

    http://www.sevenrealms.org/?page_id=99
    http://www.sevenrealms.org/?page_id=100

    I would like to be able to be able to change the color of a table cell depending on the number of posts in the category represented by that cell (making 0's white).

    I could possibly modify it myself (knowing just enough about coding to be dangerous) if I had the right coding resources. If anybody reading this doesn't happen to have a particular answer, then perhaps you could point me to somewhere on the codex or the web that would show me how to write or modify a plugin that would add this kind of functionality??

    Thanks.

  12. Kafkaesqui

    Posted 8 years ago #

    benwisdom, change the first function line to:

    function count_posts ($category = '',
    $span = '0', $display = true)

    Then towards the end of the plugin script where you find the line: echo $number; change that to:

    if($display) {
    echo $number;
    } else {
    return $number;
    }

    To assign the value:

    <?php $var = count_posts('daily log', '60', false); ?>

  13. benwisdom
    Member
    Posted 8 years ago #

    That worked! Thanks!

  14. benwisdom
    Member
    Posted 7 years ago #

    I love this plugin so much, I would like to understand how to add even more functionality:

    • Is there an easy way to modify this plugin to count posts between two arbitrary dates, instead of just between a day in the past and today? I'd like to use that to show the number of posts in each category on my archived pages for any given month or day.
    • Also, is it possible to count the number of posts in each category for a given author, instead of all posts regardless of author?

    Thanks.

  15. jimkimmons
    Member
    Posted 7 years ago #

    The author count is something I need badly. I hope someone is working on it, as I'm just learning and I'll get old trying to figure it out. What I want to do is list author links in a sidebar sorted by the number of their posts, thus moving frequent posters to the top of the link list.

  16. Chris_K
    Member
    Posted 7 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags