<?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>Cameron McKay &#187; WordPress</title>
	<atom:link href="http://cdmckay.org/blog/category/wordpress/feed/" rel="self" type="application/rss+xml" />
	<link>http://cdmckay.org/blog</link>
	<description>Programming and Game Development</description>
	<lastBuildDate>Mon, 14 May 2012 21:11:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>How to query posts by template in WordPress</title>
		<link>http://cdmckay.org/blog/2010/05/13/how-to-query-posts-by-template-in-wordpress/</link>
		<comments>http://cdmckay.org/blog/2010/05/13/how-to-query-posts-by-template-in-wordpress/#comments</comments>
		<pubDate>Thu, 13 May 2010 17:39:37 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1508</guid>
		<description><![CDATA[Ever wanted to filter a query_posts call by template? If you&#8217;re reading this post, you probably have or need to right now. You probably went to the query_posts documentation and scanned for something like &#8220;template=foo&#8221; and were deeply disappointed. Then maybe, in an act of desperation, you started to trudge around the plugin library. Stop! [...]]]></description>
			<content:encoded><![CDATA[<p>Ever wanted to filter a query_posts call by template?  If you&#8217;re reading this post, you probably have or need to right now.  You probably went to <a href="http://codex.wordpress.org/Function_Reference/query_posts">the query_posts documentation</a> and scanned for something like &#8220;template=foo&#8221; and were deeply disappointed.  Then maybe, in an act of desperation, you started to trudge around <a href="http://wordpress.org/extend/plugins/">the plugin library</a>.  Stop!  Stop right there!  You don&#8217;t need a plugin to do this.  Everything you need is already in <a href="http://wordpress.org">WordPress</a>.</p>
<p><span id="more-1508"></span></p>
<p>Let&#8217;s start with a question: how does WordPress associate templates?  The answer: as a secret custom field.  What&#8217;s a custom field, you ask?  They&#8217;re those useful little suckers under the content area in the WP editor:</p>
<p><a href="http://cdmckay.org/blog/wp-content/blogs.dir/2/files/2010/05/wordpress-custom-fields.png" rel="lightbox[1508]"><img class="aligncenter size-full wp-image-1522" title="WordPress Custom Fields" src="http://cdmckay.org/blog/wp-content/blogs.dir/2/files/2010/05/wordpress-custom-fields.png" alt="" width="300" height="406" /></a></p>
<p>But if the template is a custom field, you ask, then how come I don&#8217;t see it down there?  Well, I just said they&#8217;re secret.  After poking around WP database, I discovered the template for a page is determined by the custom field name <code>_wp_page_template</code>.  My suspicion is that anything with the prefix _wp is hidden from the WP admin area and therefore secret.</p>
<h3>Harnessing the awesome power of custom fields</h3>
<p>On the bright side, even if you can&#8217;t see the <code>_wp_page_template</code> in the custom fields area, I can assure you it&#8217;s still there.  And if it&#8217;s there, you can use it in any of your nefarious schemes.</p>
<p>Query_posts provides 3 parameters relating to custom fields: meta_key, meta_value, and meta_compare.  With those in mind, imagine you have a template called Profile (profile.php), and you want to exclude any page that is a Profile from our query.  In order to do that, you&#8217;d would make a query_posts call like so:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">query_posts<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'post_type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'page'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'meta_key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'_wp_page_template'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'meta_value'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'profile.php'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'meta_compare'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'!='</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The main thing to note here is that the template is identified by the name of the PHP file (profile.php) and not by the the name of template (Profile).</p>
<h3>Wrap-up</h3>
<p>Filtering your query_posts query by template is as easy as filtering by any custom field.  No plugin required.  Just remember: use the PHP file name as the template identifier, not the template name.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/05/13/how-to-query-posts-by-template-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

