Archive for the 'sql' Category
Showing total number of replies in bbpress
Anyways, to get the total number of replies, simply use the following little mysql query:
global $bbdb;
$totalReplies = $bbdb->get_var("SELECT COUNT(post_id) FROM " .
$bbdb->prefix . "posts WHERE poster_id=$user->ID");
?>
To get the total number of topics started, use:
global $bbdb;
$totalTopics = $bbdb->get_var("SELECT COUNT(topic_id) FROM " .
$bbdb->prefix . "topics WHERE topic_poster=$user->ID");
?>
WordPress 2.9 image changes
WordPress 2.9 has several new image enhancements. One of the biggest features is some basic image editing functionality. Another one is that you can now specify different alt text from the “caption” field. The “caption” field places a caption under the image. The “alt” text is used to describe the picture to non-seeing users (including search engines). This is a nice addition. However, I usually like my caption, alt text, and title text to be all the same, and I don’t like to have to enter it all manually or copy and paste. By default, wordpress will use an IPTC caption as its “description” field, which shows up in the title attribute of the image. This is nice, since I can add a caption in my image editing program of choice (Picasa) and then I don’t have to enter it again. Except for those pesky alt and caption fields, which are blank by default.
This is particularly important if I am uploading many pictures. So I wrote a little sql which will set the “caption” and “alt text” fields to be the same as the description. I already had this working for the “caption” field for quite some time, but getting it to work for the different alt text handling in 2.9 was a bit tricky, since I discovered that the alt text is not stored in wp_posts like the other fields. Instead it is stored in the wp_postmeta table, which is new in 2.9. Although it took me awhile to figure this out, the new table is a welcome addition. Now for each image you upload, wordpress stores meta information in this table, including the width and height, different sized versions of the file, the IPTC caption, some EXIF info and a few other goodies. This means that if you like to include EXIF info about your pictures, that doing so requires only a simple database lookup, instead of having to read the headers from the image, which should be considerably faster I would imagine.
Now for my little SQL which sets the “caption” and “alt text” to be the same as the description. I run this on my server after uploading pictures. It would be even better if I could figure out how to do this as a wordpress plugin.
-- description (post_content)
UPDATE wp_posts SET post_excerpt=post_content WHERE post_type='attachment' AND
date(post_date)=curdate();
-- Next we set the alternative text to be the same as the post_excerpt
INSERT INTO wp_postmeta (wp_postmeta.post_id, wp_postmeta.meta_value) SELECT
DISTINCT wp_posts.ID, wp_posts.post_excerpt FROM wp_posts, wp_postmeta WHERE
wp_posts.ID=wp_postmeta.post_id AND post_type='attachment' AND
date(post_date)=curdate();
-- This line sets the correct meta_key for the previous line, which doesn't
-- seem possible otherwise
UPDATE wp_postmeta SET meta_key='_wp_attachment_image_alt' WHERE meta_key IS
NULL;
Releasing the Collapsing Archives WordPress Plugin
Finally getting around to releasing some more plugins. I started using the Fancy Archives plugin by Andrew Rader about the same time I started using his Fancy Categories plugin (maybe in the reverse order actually). I have been modifying it for some time now, and it seems appropriate to release it as a new plugin. The functionality is best described by simply looking at my archives list on this blog. The default wordpress archives list is a simple unordered list. This plugin gives it some dynamic capabilities, similar to the default on Blogger. I have decided to keep increasing the version number from what Andrew was using, so I am calling this version 0.6. Here are the main highlights:
- Changed name from Fancy Archives to Collapsing Archives
- Changed author from Andrew Rader to Robert Felty
- Added option to link to archives.php
- Added option to list in chronological or reverse chronological order
- Added triangles which mark the collapsing and expanding features
That is, clicking on the triangle collapses or expands, while clicking
on a month or year links to the archives for the said month or year - Changed behavior from starting all expanded and then collapsing on page
load to the opposite - Removed the rel=’hide’ and rel=’show’ tags, because they are not xhtml
1.0 compliant. Now uses the CSS classes instead
You can download it from the WordPress plugin repository.
Releasing the Collapsing Categories WordPress Plugin
I started using the Fancy Categories plugin by Andrew Rader about 6 or 8 months ago. I have been slowly modifying it for some time now, and it seems appropriate to release it as a new plugin. (Note that I thought that Andrew Rader disappeared as well, but I now have found him at his new home at void*. The functionality is best described by simply looking at my categories list on this blog. The default wordpress categories list is a simple unordered list. This plugin gives it some dynamic capabilities, similar to the default on Blogger. I have decided to keep increasing the version number from what Andrew was using, so I am calling this version 0.2. Here are the main highlights:
- Changed name from Fancy Archives to Collapsing Archives
- Changed author from Andrew Rader to Robert Felty
- Added triangles which mark the collapsing and expanding features That is, clicking on the triangle collapses or expands, while clicking on a month or year links to the archives for the said category. This uses html entities (dings) instead of images, for a variety of reasons
- Lists the titles of posts, instead of just listing subcategories
- Removed the rel=’hide’ and rel=’show’ tags, because they are not xhtml 1.0 compliant. Now uses the CSS classes instead
- MOST IMPORTANTLY — it is compatible with both the pre 2.3 database which uses categories, and the 2.3+ database structure which uses the tag taxonomy
You can download it from the WordPress plugin repository.



