<?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>Egeste.NET &#187; codex</title>
	<atom:link href="http://www.egeste.net/tag/codex/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.egeste.net</link>
	<description>Some Random Phrase</description>
	<lastBuildDate>Mon, 23 Aug 2010 16:50:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>WordPress get_pages() meta_key woes</title>
		<link>http://www.egeste.net/2010/04/wordpress-get-pages-meta-key-woe/</link>
		<comments>http://www.egeste.net/2010/04/wordpress-get-pages-meta-key-woe/#comments</comments>
		<pubDate>Sun, 18 Apr 2010 18:11:27 +0000</pubDate>
		<dc:creator>Egeste</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Theme Development]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[codex]]></category>
		<category><![CDATA[get_pages()]]></category>
		<category><![CDATA[hierarchical]]></category>
		<category><![CDATA[meta_key]]></category>
		<category><![CDATA[no results]]></category>
		<category><![CDATA[not working]]></category>

		<guid isPermaLink="false">http://www.egeste.net/?p=5</guid>
		<description><![CDATA[TL;DR - you have to use the hierarchical=0 arg to list all pages that contain your meta_key, otherwise you will only get top-level pages.]]></description>
			<content:encoded><![CDATA[<p>Every once in a while you run into a problem that makes you scratch your head… and eventually maybe smash it against your desk a few times. This was definitely one of those.</p>
<p>get_pages() is a method that returns an array of $post objects (which is very well documented <a target="_blank" href="http://wphackr.com/wordpress-post-variable-quick-reference/">here</a>). There are multiple arguments you can pass to get_pages() for the purpose of filtering the results you get back (<a target="_blank" href="http://codex.wordpress.org/Function_Reference/get_pages">these args are well documented here</a>). For example, if you only want to retrieve sub-pages of the current page, you could say:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pages</span> <span style="color: #339933;">=</span> get_pages<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;child_of=&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>One of the args you can specify is the meta_key arg, which will only return pages that contain the meta_key you specify. (See: <a target="_blank" href="http://codex.wordpress.org/Custom_Fields">Custom Fields</a>) This is where I ran into some trouble.</p>
<p>It turns out that get_pages has an argument called hierarchical, which according to the codex does the following:</p>
<blockquote><dl>
<dt> hierarchical </dt>
<dd> (<em>boolean</em>) Display sub-Pages in an indented manner below their parent or list the Pages inline.  The default is <em>true</em> (display sub-Pages indented below the parent list item). Valid values:</p>
<ul>
<li> <tt>1 (true)</tt> &ndash; default</li>
<li> <tt>0 (false)</tt></li>
</ul>
</dd>
</dl>
</blockquote>
<p>What’s not intuitive about this argument is that it will filter subpages, (that is, not top-level pages) when using the meta_key arg. Here’s an example:</p>
<p><strong>Data Set</strong></p>
<ul>
<li>Homepage</li>
<li>About
<ul>
<li>Foo</li>
<li>Bar (This item has a meta_key named banner_title)</li>
</ul>
</li>
</ul>
<p><strong>Code</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span>get_pages<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;meta_key=banner_title&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Output:</strong> 0<br />
The output indicates that 0 results were returned, despite the fact that we have a page with a meta_key named banner_title. So lets try something different.</p>
<p><strong>Code</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span>get_pages<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;meta_key=banner_title&amp;hierarchical=0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Output: </strong>1<br />
Ah, now we’re getting the results we want.</p>
<p>TL;DR &#8211; you have to use the hierarchical=0 arg to list all pages that contain your meta_key, otherwise you will only get top-level pages.</p>
<p>-Egeste</p>
]]></content:encoded>
			<wfw:commentRss>http://www.egeste.net/2010/04/wordpress-get-pages-meta-key-woe/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
