Posted on 6 March, 2012 By 3 Comments

How to Sort WordPress Posts by a Custom Field

 

I’ve been working on a big project recently that is based on a listing of green companies that are in specific industries and regions in Norway.

One of the requirements is that certain companies need to listed at the top of a listing of all companies. Since the listings are WordPress posts, the date added, post ID and an alphabetical sort won’t accomplish this. So, I decided to create a simple custom field – companies with one value will be listed higher than ones with another value and then sorted alphabetically.

I wasn’t sure, however, how to sort by the custom field. I located the solution in the WordPress forums and learned that this can be done with a post query that includes the name of the custom field and sort by the custom field’s value in the arguments of the query.

The query would look like this:

<?php $saved = $wp_query; query_posts( ‘showposts=1000&meta_key=popularity&orderby=meta_value&order=ASC’ ); ?>
<?php if ( have_posts() ) { while ( have_posts() ) { the_post(); ?>

  1. showposts=1000 is how many posts that you would like to display. 1000 seems high but I am only displaying the title, which is the company name, and I want all of them to be listed.
  2. meta_key=popularity – this is the name of the custom field that you created and by which you want to sort the posts.
  3. orderby=meta_value – this is saying to sort the posts by the custom field you listed under meta_key
  4. order=ASC – this is just saying use an ascending order for the sort

If you want to learn more about using custom fields, Jeff Starr has a great tutorial at Perishable Press. It’s a couple of years old now but is still the one of the clearest out there.

I tend to not use Custom Fields, if possible, because I find that many clients find them a bit confusing. This issue can be resolved, however, by using the Advanced Custom Fields plugin. It makes it really easy to create a block in the editor, which can be called something like Company, and all of the fields are listed for input in this block. The plugin can also add the visual editor to specific fields and a simple image uploader. When this plugin is used, custom fields are no longer confusing, and become additional fields to use when creating a new post.

The plugin will create a nice custom field area that looks like the following.

Do you use any custom fields? Does your theme use custom fields for images, slideshows or other features? Have you ever needed to sort by a custom field?

photo credit: mr_t_in_dc


Templatic

Keep Reading:
Posted In : WordPress Tips

Did you enjoy this article? I can assist you with your WordPress or Facebook project. Contact me and I will provide you with an estimate. You can also connect with me on Facebook or follow me on Twitter. If you are looking for Hosting, WordPress Theme, Newsletter or other recommendations, please view my detailed list.




3 Responses to “How to Sort WordPress Posts by a Custom Field”

Leave a Comment
You may use: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .

CommentLuv badge

About

Kim Woodbridge is an accomplished Information and Technical Consultant specializing in the entire implementation of a WordPress based website including installation, theme design, upgrades, unique customizations and ongoing site maintenance.

Recent Comments

    • Chris Tusa: NovelSnippet.com is cool. It contains hundreds of books (and seems to be rapidly growing). The books are accompanied by ...
    • Dee: I drop a leave a response when I especially enjoy a post on a site or I have something to contribute to the conversation...
    • Kabsiha: Thanks for your great idea to hide the deactivated comment link in word press this would be the very useful post for who...
    • Pat Alexander: Well now these are really great. Makes it perfect for small sites....
    • Tracy G: I really don't know how to do it, glad you posted it here....

Wordpress Services

  • Installation, upgrades and maintenance
  • Conversion of existing html and css templates
  • Theme and plugin recommendations
  • CSS customizations
  • Troubleshooting and tweaks for unique situations
  • Customization for individual blogging goals and needs
  • Training and advice