Nothing But Words

Mike Toppa’s Blog

About | Contact | Archives | Photos | WP Plugins

My Sister, Best Bartender

Shashin Error:

No photos found for specified shortcode

A Day at the Beach

Shashin Error:

No photos found for specified shortcode

Twitter Weekly Updates for 2010-06-27

  • Just when I thought I could start blogging again, I had 2 more crazy weeks at work. But soon we're off to Newport, then Tokyo! #
  • After just one post re-launching the "political" section of my blog, I'm going to drop it. I'm just never going to have the time for it. #
  • So, please suggest an adjective for "travel" to replace the "political" section of my blog (it'll be alongside "personal" and "technical") #
  • There's only one big festival happening near Tokyo while we're there, but it looks like a good one http://bit.ly/asBYjN (scroll down) #

Twitter Weekly Updates for 2010-06-20

  • Ugh, it's so humid that when I got out of our air conditioned car, my sunglasses instantly fogged up #
  • Was hoping for a new iPhone in time for our Japan trip; failed after waiting an hour yesterday; forgot I put a freeze on my credit report #
  • New house listing in Newport from my Dad – http://www.terrytoppa.com/147-third-st #

Twitter Weekly Updates for 2010-06-13

  • Next time I'm in South Carolina I'll have to remember to add my name to the ballot for Senate – I might win! http://bit.ly/cXA3t2 #
  • Maria: "I can tell Eidan's language skills are improving because his rejoinders are getting nastier" #

Displaying WordPress Posts by Category, Even If They’re Not Recent

A couple years ago I wrote a post describing how to limit posts on your WordPress home page to ones in specific categories. A limitation of this approach is that it can only show recent posts that are in The Loop. Others have solved this problem as well, but all the solutions I’ve seen have this same limitation. What if posts in the categories you specify aren’t among your recent posts? Your home page would show no posts!

This was a problem for my new home page design. I’ve divided my site’s content into 3 major topics, and I show hyperlinked titles for the 3 most recent posts in each topic. But it’s possible that a topic may not have any posts among the most recent 10 posts, which is all The Loop knows about (10 is WordPress’ default setting for how many posts to show on your home page). So I want to get the most recent 3 posts for each topic, regardless of whether they happen to be in The Loop.

To do this, I created the following function and put it in my theme’s functions.php file. Note that I’m using a straight SQL query, which means this is not guaranteed to work in future versions of WordPress (the WP coders do a good job of maintaining a consistent programming API across versions, but they do change the database sometimes).

function get_top_category_posts ($term_ids) {
    global $wpdb;
    $top_3 = '';

    $results = $wpdb->get_results("select ID, post_title, post_date from wp_posts p
            inner join wp_term_relationships r on p.ID = r.object_id
            inner join wp_term_taxonomy t on t.term_taxonomy_id = r.term_taxonomy_id
            where t.term_id in ($term_ids) and p.post_type = 'post'
            order by post_date desc limit 3", ARRAY_A);

    foreach ($results as $result) {
        $top_3 .= "<li>" . date("M d", strtotime($result['post_date'])) . " - "
            . '<a href="' . get_permalink($result['ID']) . '">'
            . $result['post_title'] . "</a></li>\n";
    }

    return $top_3;
}

I then call it like this from my custom home page (where I’m not using The Loop at all):

<ul>
<?php echo get_top_category_posts('6,12,89,90,98,105,106,115'); ?>
</ul>

I’m passing the IDs for the categories I want. The easiest way to get a category ID is to go to its edit screen and note the cat_ID in the URL. This works for tag IDs also.

There are 3 database tables involved in the query: wp_posts contains your posts, wp_term_taxonomy contains the term IDs for categories (and tags), and wp_term_relationships connects the posts to their categories. Note that a cat_ID or tag_ID you see in an edit screen URL is actually called a term_id within the database. In this case I’m only retrieving the date and title of each post, but you could retrieve the entire post if you want (see the description of the wp_posts table).

If you want to simplify the query to improve performance, you can eliminate one of the table joins if you manually look up the term_taxonomy_id that corresponds to each category’s term_id, and pass those instead:

select ID, post_title, post_date from wp_posts
        inner join wp_term_relationships on ID = object_id
        where term_taxonomy_id in ($taxonomy_ids) and post_type = 'post'
        order by post_date desc limit 3

The downside of doing this is that every time you add a new category or tag, you’ll have to go into your database and look up its term_taxonomy_id. So I don’t recommend doing it this way unless you’re comfortable poking around in mySQL.

(Note to fellow cranky programmers: I know this is poorly abstracted – the need here is simple enough that, to me, abstraction didn’t seem worth the trouble).

Twitter Weekly Updates for 2010-06-06

  • Hard finding a WordPress social bookmarking plugin that doesn't make me a tool for the Man. No DoubleClick cookies for my visitors, please. #
  • After wasting time with AddThis and ShareThis I settled on Sociable. No surreptitious javascript or cookies. #
  • I predict Moonbase Alpha will be ready for human occupation right when Space 1999 bellbottom outfits come back in style http://bit.ly/9i0Ti8 #
  • "It is ironic that the Gores would outdo the Clintons in getting divorced… In a sense, getting divorced is the iconic baby boomer act." #
  • RT @delagoya awesome XKCD today http://bit.ly/dudJjz #

Kai’s School Activities

Kai showing off his creation at the Haverford art show for all the district's elementary schoolsKai showing off his creation at the Haverford art show for all the district’s elementary schools

Kai showing off his creation at the Haverford art show for all the district’s elementary schools02-Jun-2010 08:45, SONY DSC-W55, 2.8, 6.3mm, 0.025 sec, ISO 100

Kai, learned scholar of IslamKai, learned scholar of Islam

Kai, learned scholar of Islam02-Jun-2010 23:28, SONY DSC-W55, 4.5, 14.3mm, 0.02 sec, ISO 125

Kai is having a busy last couple of months in 3rd grade. Last month he was in a recorder concert with his music class. The video below is a small group within the class playing the song Steppin’. In the video it’s hard to understand what the teacher is saying at the beginning – he’s explaining that Kai and a few other kids found the song on the teacher’s web page and started teaching themselves how to play it (the teacher intended the song for his 5th grade class). The teacher spent a small amount of time guiding them through the trickier parts of the song, but they mostly learned it themselves.

Video - Kai and his 3rd grade classmates perform Steppin (Kai is right next to the microphone stand)Video – Kai and his 3rd grade classmates perform Steppin (Kai is right next to the microphone stand)

Video – Kai and his 3rd grade classmates perform Steppin (Kai is right next to the microphone stand)29-Apr-2010 23:29

Today Kai did a presentation on Egypt for his class, which is why he’s wearing a jalabiya in the picture above (Maria covered herself up and took him to an Islamic store in West Philadelphia to buy it for him). He also brought in a prayer rug, drew a map of Egpyt, and made a desert diorama with legos (with a combination of pieces from a SpongeBob set and a Prince of Persia set).

In April, Maria spent a lot of time and energy planning an Asian culture day for his class, and took a day out of work to manage it. Several of her students and another Villanova professor also participated. Unfortunately she was too busy to take pictures, but they had the kids:

  • Learn Tinikling dancing from the Phillippines
  • Create Mali mud cloth
  • Make Vietnamese spring rolls
  • Learn some basics of Islamic etiquette, and learn how to write their names in Arabic
  • Do an international trade simulation. Maria put this together and it sounded amazing. She had the kids break into groups and pretend to be different countries. Each country had to make various shapes out of paper, as a way to represent their economic output. Some groups got a lot of tools like scissors, rulers, etc, others got a few tools, and some got none at all. They then had a time limit to make as many high quality shapes as they could. I’m forgetting some of the specifics at this point, but they then had a way to pretend to sell their creations, and use the money to expand their tool kits. A key point was to illustrate that it’s hard for a country to grow it’s economy (especially in relative terms) , even when it tries hard, when it starts from a weak position. While it’s a simplified exercise, it’s also an impressive way to get kids to begin comprehending a complex topic.

You are currently browsing the Nothing But Words blog archives for June, 2010.