When your website means business.

«Return to Blog List Help from a LinkedIn Group: Ordering WordPress Custom Field Items

In general, I’m not as thrilled with LinkedIn as I had hoped I’d be. Like a lot of social media platforms, it draws its share of spammers, shills, and snake-oil salespeople. However, one LinkedIn group I belong to has proven itself to be of benefit time and again: the WordPress group. I’ve gotten useful answers for several WordPress issues I’ve faced over the last several months. There are some really capable and helpful people in the group. I was once given a solution to a problem by Mike Little, who I recognized only later is the guy who, with Matt Mullenweg, started the WordPress project.

Most recently, I needed a way to order WordPress custom fields easily (easily enough that it made sense to clients who would have to maintain their site). By default, custom field items are displayed in the order in which they are created. This is a problem if you want to add a new item and display it at the top of the list.

Mike Schinkel, a web marketing strategist from Atlanta, stepped in with a solution. (How cool is that? I can get coding assistance from someone several hundred miles away!) Mike Schinkel is one of the more active and helpful folks on the LinkedIn WordPress group, and he’s worth connecting with and following. He runs a WordPress business conference (among other conferences) and is Executive Director of Startup Atlanta. I’d guess he’s fairly busy, but he takes time help people like me with limited coding skills.

His solution is so clean and simple I wanted to share it here. One of the beauties of it is that, not only does it organize the custom field items on the page in the order you want, but it also organizes the items in order in the page admin area, taking advantage of WordPress’ default alpha organization.

Essentially, you create custom field items with names like so: item-1, item-2, item-3, etc. As you might guess, item-1 is meant to show first on the custom field listing. Want to make item-1 show second? Change the custom field name to item-2, then rename the other items to complete the reorganization (you can’t have more than one value for each name in this scenario).

On the page template, the code to call the items in order looks like this:


< ?php
     for($i=1; true; $i++) {
     $item = get_post_meta($post->ID, "item-$i", true);
     if (empty($item))
     break;
     echo '
'.$item.'
'; } ?>

I used this to display thumbnails and a short description for artwork on a recently launched artist’s website. As she adds new pieces, or if she decides to emphasize different pieces on a category page, she can easily rename the existing custom fields to reorganize items.

One of the caveats of this solution is that the custom field names must start with -1 and be sequential. Going from "item-1" to "item-3", with no "item-2", breaks it.

ShareShare on FacebookShare on Google+Share on RedditTweet about this on TwitterShare on LinkedInEmail this to someone

Tags: ,

Comments are closed.