Anyone interested in a post count function?
-
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
-
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.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;
}
?>By the way, this function also works without a category selected, or without a time span selected. π
Anonymousxtra cool. I think this is pretty useful and it looks great on my blog.
thank you, man.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/
tooGreat code, thanks π
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.phpsClint & 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’ “);I have fixed this in the source I made available for the plugin :
http://www.carthik.net/wpplugins/count_posts.phps
Thanks!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=100I 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.
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); ?>
That worked! Thanks!
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.
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.
- The topic ‘Anyone interested in a post count function?’ is closed to new replies.