Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Clint

    (@clint)

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

    Thread Starter Clint

    (@clint)

    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;
    }
    ?>

Viewing 2 replies - 1 through 2 (of 2 total)