<?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>damontimm.com &#187; wordpress</title>
	<atom:link href="http://blog.damontimm.com/tag/wordpress/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.damontimm.com</link>
	<description>Where I go to remember what I did</description>
	<lastBuildDate>Fri, 16 Jul 2010 18:51:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to: Retreive Custom Field Data in a WordPress Post</title>
		<link>http://blog.damontimm.com/how-to-retreive-custom-field-data-in-a-wordpress-post/</link>
		<comments>http://blog.damontimm.com/how-to-retreive-custom-field-data-in-a-wordpress-post/#comments</comments>
		<pubDate>Sun, 19 Oct 2008 22:00:39 +0000</pubDate>
		<dc:creator>Damon</dc:creator>
				<category><![CDATA[how to]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.damontimm.com/blog/?p=72</guid>
		<description><![CDATA[Purpose: use WordPress&#8217;s &#8220;Custom Field&#8221; key/data pairs to store and retrieve specific post information and display it in your own custom templates. how this works I like to store post information in the form of key/data pairs utilizing WordPress&#8217;s &#8220;Custom Fields&#8221; for posts. You can make up any particular key/data pair you desire &#8212; I [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Purpose:</strong> use WordPress&#8217;s &#8220;Custom Field&#8221; key/data pairs to store and retrieve specific post information and display it in your own custom templates.</p>
<p><span id="more-72"></span></p>
<h3>how this works</h3>
<p>I like to store post information in the form of key/data pairs utilizing WordPress&#8217;s &#8220;Custom Fields&#8221; for posts.  You can make up any particular key/data pair you desire &#8212; I choose, in this instance, to use key/data pairs to supplement some event information in some of my posts.  You can see an example of this at <a href="http://www.rosaleeshow.com/2008/10/12/indianapolis-in-2/">rosaleeshow.com</a>.</p>
<p>(To implement this, you will need some familiarity with customizing a wordpress theme.)</p>
<p>First, I created a function that takes an array of keys as an argument; these are the key values the function will search for in the post meta data looking for custom field information.  If a matching key is found, it will display that value in a definition list.  </p>
<p>I used this function called <code>eventmetadl()</code>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">//first argument: current post; </span>
      <span style="color: #666666; font-style: italic;">//second argument, array of keys to search through</span>
<span style="color: #000000; font-weight: bold;">function</span> eventmetadl<span style="color: #009900;">&#40;</span><span style="color: #000088;">$postID</span><span style="color: #339933;">,</span> <span style="color: #000088;">$metaArray</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
&nbsp;
  <span style="color: #666666; font-style: italic;">//this array will hold the key/data if matches are found</span>
  <span style="color: #000088;">$metaList</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//search through each item passed to the function </span>
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$metaArray</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$meta</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//for each key, get the data</span>
    <span style="color: #000088;">$metadata</span> <span style="color: #339933;">=</span> get_post_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$postID</span><span style="color: #339933;">,</span> <span style="color: #000088;">$meta</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
    <span style="color: #666666; font-style: italic;">//if there is data then add that key/data to the list</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$metadata</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">NULL</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
      <span style="color: #000088;">$metaList</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$meta</span>&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$metadata</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//if there is a list of meta data then:</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$metaList</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;dl&gt;&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//open the definition list, etc</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//go through each item and print title/defition</span>
    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$metaList</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$dt</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$dd</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;dt&gt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$dt</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;:&lt;/dt&gt;&lt;dd&gt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$dd</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/dd&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;/dl&gt;&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// close the definition list</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Then, inside my <code>sidebar.php</code> file in my theme, I create an array of the keys I am looking for and call my function to create the definition list.  If no keys are found to match, no list is created.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">// in my sidebar.php file - could be in any template</span>
  <span style="color: #000088;">$metatags</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Location&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Ticket Price&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Contact&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Website&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  eventmetadl<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #000088;">$metatags</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>I have used this technique with considerable success &#8212; however, I am sure I stole the idea from someone else.  Thanks to that person, wherever they may be.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.damontimm.com/how-to-retreive-custom-field-data-in-a-wordpress-post/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
