<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Elias Interactive &#187; custom fields</title>
	<atom:link href="http://www.eliasinteractive.com/blog/tag/custom-fields/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.eliasinteractive.com</link>
	<description>Ecommerce consulting &#38; solutions for online store retailers</description>
	<lastBuildDate>Thu, 01 Sep 2011 18:45:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Sorting Data from Custom Fields in WordPress</title>
		<link>http://www.eliasinteractive.com/blog/sorting-data-from-custom-fields-in-wordpress/</link>
		<comments>http://www.eliasinteractive.com/blog/sorting-data-from-custom-fields-in-wordpress/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 10:00:43 +0000</pubDate>
		<dc:creator>Luke Whitson</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[custom fields]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.eliasinteractive.com/?p=898</guid>
		<description><![CDATA[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 &#8230; <a href="http://www.eliasinteractive.com/blog/sorting-data-from-custom-fields-in-wordpress/">Continue Reading <span class="meta-nav">&#187;</span></a>]]></description>
			<content:encoded><![CDATA[<p>With the redesign of the Ei Site, we wanted to take advantage of certain features native to WordPress, one of which was Custom Fields.</p>
<p>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:</p>
<p><strong>Name:</strong> client<br />
<strong>Value:</strong> Name|Work_Done|Description|URL</p>
<p>You can use the <em>get_post_meta()</em> function to loop through all of the &#8216;client&#8217; fields and pull the value for each and display to your page.</p>
<p><strong>ORIGINAL CODE:</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
	$allOptions = get_post_meta($post-&gt;ID, 'client', false);
	if($allOptions) {

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

	}
?&gt;
</pre>
<p>The problem comes if you want to control the way the output is displayed.  The <em>get_post_meta()</em> 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 &#8216;sort order&#8217;.</p>
<p><strong>Name</strong>: client<br />
<strong>Value:</strong> Sort|Name|Work_Done|Description|URL</p>
<p>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.</p>
<p><strong>NEW CODE:</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
	$client_array = array();
	$allOptions = get_post_meta($post-&gt;ID, 'client', false);
	if($allOptions) {
		foreach ($allOptions as $option) {
			$fullValue = explode (&quot;|&quot;, $option);
			$order = $fullValue[0];
			$client_array[$order] = $option;
		}
	}
	rsort($client_array, SORT_NUMERIC);
?&gt;
</pre>
<p>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.
<div id="tweetbutton898" class="tw_button" style="float:left;margin-right:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fbit.ly%2FmOGT4v&amp;via=tweetelias&amp;text=Sorting%20Data%20from%20Custom%20Fields%20in%20WordPress&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.eliasinteractive.com%2Fblog%2Fsorting-data-from-custom-fields-in-wordpress%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.eliasinteractive.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>
]]></content:encoded>
			<wfw:commentRss>http://www.eliasinteractive.com/blog/sorting-data-from-custom-fields-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

