Elias
ourservices
ourmodules
ourwork
ourteam
ourblog
« Go Back

Nov 17

Tuesday

Sorting Data from Custom Fields in WordPress

4
Comment(s)


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

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.

  • Twitter
  • Facebook
  • Delicious
  • LinkedIn
  • Digg
  • StumbleUpon
  • Technorati Favorites
  • Google Reader
  • Gmail
  • Tumblr
  • Blogger Post
  • Share/Save

Tags: custom fields, Development, wordpress 

4 Comments



meda says:

November 29th, 2009 at 7:48 pm


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


Luke Whitson says:

November 29th, 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.


meda says:

November 30th, 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


Sorting image from custom fields « heidzir says:

January 20th, 2010 at 8:30 am


[...] Solution from others: Sorting Data from Custom Fields in WordPress [...]

Leave a comment




RSSNew Here?

You should grab our RSS Feed to get updates.


RSSSearch Our Blog



TagsTag Cloud

    business tools coda-slider consulting copy create unique prefix for order numbers custom customize magento custom magento delete test orders Design design content Development dropbox ecommerce stores email marketing fancybox featured products featured products on home page install magento integration Magento Magento advice Magento Commerce magento customization Magento Custom Module magento custom theme magento developer magento development magento ecommerce Magento help magento installation magento modification magento online store magento open source Magento Payment Method magento service provider magento services magento template magento theme magneto ecommerce marketing magento modify magento open source ecommerce scene 7 startup




aboutus

It's simple. Elias Interactive exists to help you get the most out of your software - be it a company website or ecommerce store. Our team is a leader in the popular Magento ecommerce community, works with a variety of CMS solutions, and builds elegant UI designs. We are a small, talented group of tech fanatics. We have an uncompromising commitment to do the right thing. And we are passionate about what we do - changing the world one line of code at a time.


clienttestimonials

videoWhy Elias?
videoWould You Recommend Elias?



Let's Talk


recentposts

Rework Book Critique

Rework is exactly what you would expect from 37signals - simple, easy to read, and "at home good". About 80% of the book's ideas are already accessible in keynotes and interviews. But a succinct,...
Continue Reading »

sep
Pandora persevered: NY Times quote

From March 7, 2010 New York Times article about Pandora radio: It is all a long way from January 2000, when Mr. Westergren founded . Trained as a jazz pianist, he spent a decade playing in rock...
Continue Reading »

sep

» Read More

needsupport?

ContactSubmit a Request
CampfireLive Support is Unavailable at this time



twitterupdates

Does your startup pass The Sleep Test? http://bit.ly/d8QYBS
1 day ago

Thanks @jamespaden for the RT
1 week ago

New Elias #magento module store live with 1st module for sale: configurable bundle products. http://bit.ly/9F1T69
1 week ago

Posting tweet...


» Follow us to stay in the loop





Home | Our Services | Our Modules | Our Work | Our Team | Our Blog

Copyright © 2008-2009 Elias Interactive. All rights reserved.