• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Elias Interactive

We Grow Beautiful Websites

  • Blog
  • Show Search
Hide Search

Sorting Data from Custom Fields in WordPress

Luke Whitson · Nov 17, 2009 · 9 Comments

With the redesign of the Ei Site, we wanted to take advantage of certain features native to WordPress, one of which was Custom Fields.

Having a custom web design based on the needs of your particular business and customers optimizes conversion rates of site visitors, a key aspect of growing your business and brand presence online

We wanted a simple way to display our client list using Custom Fields. Using built-in WordPress functions, it is pretty simple to pull information from Custom Fields. With Custom Field structure like this:

Name: client
Value: Name|Work_Done|Description|URL

You can use the get_post_meta() function to loop through all of the ‘client’ fields and pull the value for each and display to your page.

ORIGINAL CODE:

<?php
	$allOptions = get_post_meta($post->ID, 'client', false);
	if($allOptions) {

		foreach ($allOptions as $option) {
			$fullValue = explode ("|", $option);
			$name = $fullValue[0];
			$work = $fullValue[1];
			$text = $fullValue[2];
			$url = $fullValue[3];
		}

	}
?>

The problem comes if you want to control the way the output is displayed. The get_post_meta() function does not provide a SORT property. So in order to control the order of display, we simply added an additional component to the VALUE field, a ‘sort order’.

Name: client
Value: Sort|Name|Work_Done|Description|URL

Rather than just display the information in the initial foreach() loop, we stored the data into a new array, based on the SORT item for use later.

NEW CODE:

<?php
	$client_array = array();
	$allOptions = get_post_meta($post->ID, 'client', false);
	if($allOptions) {
		foreach ($allOptions as $option) {
			$fullValue = explode ("|", $option);
			$order = $fullValue[0];
			$client_array[$order] = $option;
		}
	}
	rsort($client_array, SORT_NUMERIC);
?>

With the data now stored in an sorted array, we can now loop through the new array and display the information in any order we want.

Share this:

  • Tweet
  • Pocket
  • More
  • Email

Blog, Development custom fields, Development, wordpress

Reader Interactions

Comments

  1. meda says

    November 29, 2009 at 7:48 pm

    A practical example would be nice for those like me who know very little about php.

    Reply
  2. Luke Whitson says

    November 29, 2009 at 7:51 pm

    meda, that is the exact code I used. so it is as practical as it gets. what else would you like to see to help you out? let me know and i will be glad to assist.

    Reply
  3. meda says

    November 30, 2009 at 2:36 am

    Although I didn’t really get what adding SORT does to your array I’d like to see how do you display it on your blog to see if it clear things up.

    Also, I see that you sort values within one post but how would you go about sorting posts using custom fields? I’d like to display posts using a date custom fields. The loop would show posts sorted by the dates used in the custom fields. Thx

    Reply
  4. Ray Gulick says

    May 14, 2010 at 5:20 pm

    Lost me somewhere, too. I don’t understand how to attach the numeric value to a particular custom field. Probably easy, but some of us need a more step-by-step explanation.

    Reply
  5. Jermaine Oppong says

    October 30, 2010 at 10:12 pm

    You CAN also put the Name and Value in the same custom-field. Did you consider this method?
    http://www.graphicbeacon.com/how-to-split-and-categorize-wordpress-custom-field-values/

    Reply
  6. Klaus says

    October 31, 2010 at 9:55 am

    I didn’t get it … could you please give an complete example for the output / loop?

    Reply
  7. Elliott says

    October 27, 2011 at 8:37 pm

    Thanks so much for this! It really helped me out. I couldn’t for the life of me wrap my head around getting the function to sort my custom fields. 

    Reply
  8. Anne Seiler says

    February 8, 2012 at 1:22 pm

    HI, thank you for your post. I have one question. How do i  loop through the new array and display the information. Can you give me code snipet for that. I am a newbie and totaly lost. Thanks Anne

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

From the blog

Magento SVN Usage – Best Practices

How to Create Reusable Apple Mail Templates [video]

I’d rather buy from Harry Potter

The Ecommerce Solution You’ll Find Refreshing (drumroll)

Liberating Constraints

More Posts

Connect with us

  • Facebook
  • RSS
  • Twitter
Affiliate Disclaimer

© 2025 · Elias Interactive · Built on the Genesis Framework

  • Blog
  • Affiliate Disclaimer
  • Home