redirecting to a wordpress post based on custom fields

Over at Wine Library TV, we number each episode (episode #608 just went live). To make this happen on the WordPress side (beyond just the title), we take advantage of WordPress Custom Fields (a really awesome feature).

Recently, I had the need to take in an episode number and redirect to that episode’s post page. For example, if they enter 608, they should go here: I did a little bit of searching and didn’t find what I was looking for, so here’s the script (more or less) I ended up writing.

(If you can’t see it, visit it here:

It’s pretty simple. I take the post number and do a lookup in the wp_postmeta table for an entry that has the meta_value of the number and meta_key of “episode” (our internal name, sneaky huh?). Then I make a call to get_permalink using the returned post_id. If all goes well, the visitor is redirected.