<?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>Jeffri Hong &#187; Wordpress</title>
	<atom:link href="http://jeffri.net/category/wordpress/feed/" rel="self" type="application/rss+xml" />
	<link>http://jeffri.net</link>
	<description> </description>
	<lastBuildDate>Thu, 05 Apr 2012 23:49:25 +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>Sort by latest post for wp_list_categories</title>
		<link>http://jeffri.net/2012/01/sort-by-latest-post-for-wp_list_categories/</link>
		<comments>http://jeffri.net/2012/01/sort-by-latest-post-for-wp_list_categories/#comments</comments>
		<pubDate>Wed, 25 Jan 2012 20:18:53 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[source code]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=816</guid>
		<description><![CDATA[One of my developer friend asked me if we could sort by last post using wp_list_categories function for WordPress. By default, wp_list_categories accept arguments for order by ID, name, slug, count or term_group. Order by latest post is not possible by default, but with a little of tweak using filter hook, we can. First, we&#8217;ll [...]]]></description>
			<content:encoded><![CDATA[<p>One of my developer friend asked me if we could sort by last post using <a href="http://codex.wordpress.org/Template_Tags/wp_list_categories" target="_blank"><em>wp_list_categories</em></a> function for WordPress. By default, <em>wp_list_categories</em> accept arguments for order by <em>ID</em>, <em>name</em>, <em>slug</em>, <em>count</em> or <em>term_group</em>. Order by latest post is not possible by default, but with a little of tweak using filter hook, we can. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>First, we&#8217;ll looking at the function <em>wp_list_categories</em>. This function made call to <em>get_categories</em> to get the list of categories, which made another call to <em>get_terms</em>. Categories in WordPress is basically <em>terms</em> with category <em>taxonomy</em>. Finally, looking on the <em>get_terms</em> function, we will find some delicious filter hook that suitable for our customization.</p>
<p><span id="more-816"></span></p>
<p>To be able to change the order, we will have to find a way to manipulate the SQL query and add our magic. Finally, we found the filter hook to add the magic, <em><strong>terms_clauses</strong></em>. The code to call the apply filter is as below:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pieces</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'fields'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'join'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'where'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'orderby'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'order'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'limits'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$clauses</span> <span style="color: #339933;">=</span> apply_filters<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'terms_clauses'</span><span style="color: #339933;">,</span> <span style="color: #990000;">compact</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$pieces</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$taxonomies</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>As you can see, the $pieces hold a separate pieces of the SQL query. We can add our own query to this separate pieces to form a query for our need. The code we used is as follow:</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> filter_term_sort_by_latest_post_clauses<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$pieces</span><span style="color: #339933;">,</span> <span style="color: #000088;">$taxonomies</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'category'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$taxonomies</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'orderby'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'latest_post'</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$pieces</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'fields'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;, MAX(p.post_date) AS last_date&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$pieces</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'join'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot; JOIN <span style="color: #006699; font-weight: bold;">$wpdb-&gt;term_relationships</span> AS tr JOIN <span style="color: #006699; font-weight: bold;">$wpdb-&gt;posts</span> AS p ON p.ID=tr.object_id AND tr.term_taxonomy_id=tt.term_taxonomy_id&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$pieces</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'where'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot; GROUP BY t.term_id&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$pieces</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'orderby'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ORDER BY last_date&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$pieces</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'order'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DESC&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// DESC or ASC</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$pieces</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'terms_clauses'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'filter_term_sort_by_latest_post_clauses'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Add this to <em>functions.php</em> or on your own plugin code, or where ever you wanted, it just needed to be placed before any call of <em>wp_list_categories</em>. Note that we only add this filter if the taxonomy is category, and if the <em>orderby</em> argument is set to <em>latest_post</em>. So our call to <em>wp_list_categories</em> is now:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">wp_list_categories<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'orderby'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'latest_post'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>Now your <em>wp_list_categories</em> call will have option to sort by latest post. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Hope it&#8217;s useful for you. Comments is appreciated. <img src='http://jeffri.net/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2012/01/sort-by-latest-post-for-wp_list_categories/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Blog successfully updated to WordPress 3.2</title>
		<link>http://jeffri.net/2011/07/blog-successfully-updated-to-wordpress-3-2/</link>
		<comments>http://jeffri.net/2011/07/blog-successfully-updated-to-wordpress-3-2/#comments</comments>
		<pubDate>Wed, 06 Jul 2011 21:10:48 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[upgrade]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=784</guid>
		<description><![CDATA[Here is another major update from WordPress. Now, this blog has been successfully updated to 3.2. Thanksfully, nothing need to be changed in the theme. A lot of changes has been made for this version, and one that is noticeable is the new look and layout for the administration area. It looks a little better. [...]]]></description>
			<content:encoded><![CDATA[<p>Here is another major update from WordPress. Now, this blog has been successfully updated to 3.2. Thanksfully, nothing need to be changed in the theme. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>A lot of changes has been made for this version, and one that is noticeable is the new look and layout for the administration area. It looks a little better. The sidebar is changed. The color of the boxes also changed a bit, it is more light. Some fonts are also changed. Overall, I liked the new look. <img src='http://jeffri.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>There&#8217;s no yet compatibility issue so far is all that I can tell.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2011/07/blog-successfully-updated-to-wordpress-3-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress Snippets part 1</title>
		<link>http://jeffri.net/2011/05/wordpress-snippets-part-1/</link>
		<comments>http://jeffri.net/2011/05/wordpress-snippets-part-1/#comments</comments>
		<pubDate>Tue, 10 May 2011 07:33:27 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=764</guid>
		<description><![CDATA[Well, this will be a short list of WordPress snippets that we could use. This snippets is collected from my daily work on WordPress and should be useful for daily basis. WordPress mail snippets WordPress has its own improved function for sending email, it is wp_mail. We should always use this function if we wanted [...]]]></description>
			<content:encoded><![CDATA[<p>Well, this will be a short list of WordPress snippets that we could use. This snippets is collected from my daily work on WordPress and should be useful for daily basis.</p>
<h2>WordPress mail snippets</h2>
<p>WordPress has its own improved function for sending email, it is <a href="http://codex.wordpress.org/Function_Reference/wp_mail">wp_mail</a>. We should always use this function if we wanted to send email within WordPress. With this function, we don&#8217;t need to worry much about the mail header and stuff. More over, there is plenty of filter we can use to customize it.</p>
<p><span id="more-764"></span></p>
<h3>Set content type to HTML</h3>
<p>We can send HTML email by set the content type of email sent by WordPress. To do this, add this function to functions.php in your theme file.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> set_email_html_type<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$content</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'text/html'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_mail_content_type'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'set_email_html_type'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>Set mail sender name</h3>
<p>This function will set the mail sender name.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> set_mail_from_name<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$name</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;Jeffri Hong&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_mail_from_name'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'set_mail_from_name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>We could improve it by automatically set it to the first administrator name.</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> set_mail_from_name<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$name</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$admins</span> <span style="color: #339933;">=</span> get_users<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'role'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'administrator'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$admins</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$first_name</span> <span style="color: #339933;">=</span> get_user_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$admins</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'first_name'</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>
		<span style="color: #000088;">$last_name</span> <span style="color: #339933;">=</span> get_user_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$admins</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'last_name'</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>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$first_name</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$last_name</span> <span style="color: #009900;">&#41;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$first_name</span> <span style="color: #006699; font-weight: bold;">$last_name</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;Jeffri Hong&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// the default value when it is failed to retrieve first/last name</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_mail_from_name'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'set_mail_from_name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>Set mail sender email</h3>
<p>This function will set the mail sender email address.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> set_mail_from<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$email</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;info@jeffri.net&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_mail_from'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'set_mail_from'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>As with the previous one, it is also possible to automatically set it to the first administrator email.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> set_mail_from<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$email</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$admins</span> <span style="color: #339933;">=</span> get_users<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'role'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'administrator'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$admins</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$admins</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_email</span> <span style="color: #009900;">&#41;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$admins</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_email</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;info@jeffri.net&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_mail_from'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'set_mail_from'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>Get post meta ordered by id</h2>
<p>Getting a post meta, especially an array, can some time return an unexpected result. The problem is the <a href="http://codex.wordpress.org/Function_Reference/get_post_meta">get_post_meta</a> function rely on the default SQL order. In few of my job, I need to use multiple post meta and keep it in order with each order, and it won&#8217;t work correctly unless we add the ORDER BY clause to the query. Unfortunately, the metadata functions didn&#8217;t have many filters we can work with. However, there is a filter we can use to override the function return value.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> filter_post_meta<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$object_id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$meta_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$single</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_results</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT m.meta_value FROM <span style="color: #006699; font-weight: bold;">$wpdb-&gt;postmeta</span> m WHERE m.meta_key = '<span style="color: #006699; font-weight: bold;">$meta_key</span>' AND m.post_id = '<span style="color: #006699; font-weight: bold;">$object_id</span>' ORDER BY m.meta_id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$ret</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>
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$results</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$result</span> <span style="color: #009900;">&#41;</span>
		<span style="color: #000088;">$ret</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">meta_value</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$ret</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'get_post_metadata'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'filter_post_meta'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>With this, every call of get_post_meta will be filtered by this function and return all post meta filtered by meta_id. The good news is, this filter run on top of get_metadata function and returned it immediately when there is a value.</p>
<h2>Add post slug as CSS class to page lists</h2>
<p>Often, we need to have a more definitive and unique class for page lists generated by function like <a href="http://codex.wordpress.org/Function_Reference/wp_list_pages">wp_list_pages</a> and other. By default, there is unique class for the page ID, but since page ID is generated automatically, this value vary on site to site &#8211; for example when you have a development version and live version of a site, you will need an alternative for unique class other than ID. Post slug is the better solution in this case, since it is defined by us and unique.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> add_page_css_class<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$classes</span><span style="color: #339933;">,</span> <span style="color: #000088;">$page</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$classes</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'page-'</span><span style="color: #339933;">.</span><span style="color: #000088;">$page</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_name</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$classes</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'page_css_class'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'add_page_css_class'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Add this function to functions.php of your themes. Now, you will have one more class selector for your CSS.</p>
<h2>And more to come</h2>
<p>That&#8217;s it folks for the part 1. More will come in near future as I still have many interesting case I experienced and a nice solution that is re-usable anywhere. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Hope that useful for you.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2011/05/wordpress-snippets-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Customize Custom Post Type Landing Page with Clean URL</title>
		<link>http://jeffri.net/2011/05/customize-custom-post-type-landing-page-with-clean-url/</link>
		<comments>http://jeffri.net/2011/05/customize-custom-post-type-landing-page-with-clean-url/#comments</comments>
		<pubDate>Wed, 04 May 2011 01:00:32 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[post type]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=672</guid>
		<description><![CDATA[WordPress has been long capable on using custom post type. Since WordPress 3.0, we are now easier to create a custom post type. I have blogged some tips about it. However, many didn&#8217;t know that the custom post type already had it&#8217;s own landing page, though, you will need to pass the post type variable [...]]]></description>
			<content:encoded><![CDATA[<p>WordPress has been long capable on using custom post type. Since WordPress 3.0, we are now easier to create a custom post type. I have <a href="http://jeffri.net/2010/07/some-useful-tips-for-wordpress-custom-post-type-and-taxonomy/">blogged some tips about it</a>.</p>
<p>However, many didn&#8217;t know that the custom post type already had it&#8217;s own landing page, though, you will need to pass the post type variable to the URL. This post will guide you to the step by step on customizing the post type landing page, as well as adding a new rewrite rule for a cleaner URL.</p>
<p><span id="more-672"></span></p>
<h2>Register the Post Type</h2>
<p>First of all, let&#8217;s create our custom post type. In this example, we will register a <em>book</em> post type.</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> theme_init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$labels</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Books'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'singular_name'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Book'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'add_new'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Add Book'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'add_new_item'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Add New Book Item'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'edit_item'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Edit Book Item'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'new_item'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'New Book Item'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'view_item'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'View Book Item'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'search_items'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Search Books'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'not_found'</span> <span style="color: #339933;">=&gt;</span>  __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Nothing found'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'not_found_in_trash'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Nothing found in Trash'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
		<span style="color: #0000ff;">'parent_item_colon'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    	<span style="color: #0000ff;">'labels'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$labels</span><span style="color: #339933;">,</span>
    	<span style="color: #0000ff;">'public'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    	<span style="color: #0000ff;">'publicly_queryable'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    	<span style="color: #0000ff;">'show_ui'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> 
    	<span style="color: #0000ff;">'query_var'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    	<span style="color: #0000ff;">'rewrite'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    	<span style="color: #0000ff;">'capability_type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'post'</span><span style="color: #339933;">,</span>
    	<span style="color: #0000ff;">'hierarchical'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span>
    	<span style="color: #0000ff;">'menu_position'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span>
    	<span style="color: #0000ff;">'supports'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'editor'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'author'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'thumbnail'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'excerpt'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'comments'</span><span style="color: #009900;">&#41;</span>
  	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  	register_post_type<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'init'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'theme_init'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Add this code to the theme functions.php. We hook the function to the <em>init</em> action. In case you didn&#8217;t know the WordPress hook, <a href="http://codex.wordpress.org/Plugin_API">you can take a good read on codex</a>.</p>
<p>The post type is now ready to use, it appeared on the back-end, you can add, edit and remove the book. The single post is accessible now. The landing page is accessible by appending <strong><em>?post_type=book</em></strong> to your website URL.</p>
<h2>Clean the URL</h2>
<p>Now, we want to make the URL cleaner. Instead of accessing the landing page with <em>yourwebsite.com/?post_type=book</em>, we want to make it <em>yourwebsite.com/book</em> instead. We can do this by adding a new rewrite rule.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> theme_rewrite<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	add_rewrite_rule<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book/?$'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'index.php?post_type=book'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'top'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	add_rewrite_rule<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book/page/(\d+)/?$'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'index.php?post_type=book&amp;paged=$matches[1]'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'top'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">//flush_rewrite_rules(); // run one time only</span>
<span style="color: #009900;">&#125;</span>
add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'init'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'theme_rewrite'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Again, add this to the theme functions.php file. We add a new rewrite rule and redirect it to <strong><em>index.php?post_type=book</em></strong>, don&#8217;t forget to use <em>&#8216;top&#8217;</em> in the third parameter, this is to make sure this rule must be run before any other WordPress rule. We also add another rule to work with pagination.</p>
<p>Notice the commented line above? After we add a new rewrite rule, we must flush the rewrite rules. This must be done one time when it is changed. So when you have it run once, comment or remove that line as it is not needed anymore. The rules will also flushed when you go to Settings -> Permalinks in back-end.</p>
<p>However, the add rewrite rule line must be keep though, as if you remove it, the next time it flushed, you will lost the rule.</p>
<h2>Customizing the Landing Page</h2>
<p>The current landing page used the same template as the home page, which in most case is either <em>front-page.php</em>, <em>home.php</em> or <em>index.php</em>. This isn&#8217;t good as they are shared with any other post type. So, we must create a new filter, to look for <em>home-book.php</em> before falling to another template.</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> theme_home_template<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$template</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> get_query_var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'post_type'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$new_template</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'home-'</span><span style="color: #339933;">.</span>get_query_var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'post_type'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.php'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$templates</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #000088;">$new_template</span><span style="color: #339933;">,</span>
			<span style="color: #000088;">$template</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> locate_template<span style="color: #009900;">&#40;</span><span style="color: #000088;">$templates</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$template</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'home_template'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'theme_home_template'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Add this code again to theme&#8217;s functions.php. Then, create the <em>home-book.php</em> and customize it to the way you need. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2>Conclusion</h2>
<p>That&#8217;s it. So to conclude it, we first register the post type as usual, then adding a rewrite rule for the sake of cleaner URL. Finally, we create a new <em>home-{post_type}.php</em> file to serve as the template of the landing page. Now that we have the template, we can customize it to every way we wanted.</p>
<p>A different approach can be done though, such as creating a page and a custom template that query to the post type. I have used this on some of my clients. The downside is we will create a blank page, not that it&#8217;s bad, but it&#8217;s always good to not leave any <em>junk</em> in the administration back-end &#8211; so user can&#8217;t remove it accidentally and break the site.</p>
<p>This approach, although a little tricky, is a good way to make everything work seamlessly without any configuration needed. <img src='http://jeffri.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2011/05/customize-custom-post-type-landing-page-with-clean-url/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Filter get_terms to return only terms with published post</title>
		<link>http://jeffri.net/2010/08/filter-get_terms-to-return-only-terms-with-published-post/</link>
		<comments>http://jeffri.net/2010/08/filter-get_terms-to-return-only-terms-with-published-post/#comments</comments>
		<pubDate>Sat, 21 Aug 2010 22:37:03 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=609</guid>
		<description><![CDATA[So you are using custom taxonomy. You add terms to the taxonomy, and add the terms to your posts. Then, you use get_terms and list all your terms. Everything works fine, terms that doesn&#8217;t have any posts will not be returned if you set hide_empty argument to true. Now you move some of your posts [...]]]></description>
			<content:encoded><![CDATA[<p>So you are using custom taxonomy. You add terms to the taxonomy, and add the terms to your posts. Then, you use <strong>get_terms</strong> and list all your terms. Everything works fine, terms that doesn&#8217;t have any posts will not be returned if you set <em>hide_empty</em> argument to true. Now you move some of your posts to draft and you get a problem. Terms that doesn&#8217;t have any published posts still returned. This lead to 404 error when you click on the link of this term. It doesn&#8217;t look good now, we need to remove term that doesn&#8217;t have any published post.</p>
<p>So how to do that? The answer is by using WordPress filter. This piece of code below will solve the problem.</p>
<p><span id="more-609"></span></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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_terms_filter<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$terms</span><span style="color: #339933;">,</span> <span style="color: #000088;">$taxonomies</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$taxonomy</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$taxonomies</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$terms</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$terms</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$terms</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$filtered_terms</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>
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$terms</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$term</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_var</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT COUNT(*) FROM <span style="color: #006699; font-weight: bold;">$wpdb-&gt;posts</span> p JOIN <span style="color: #006699; font-weight: bold;">$wpdb-&gt;term_relationships</span> rl ON p.ID = rl.object_id WHERE rl.term_taxonomy_id = <span style="color: #006699; font-weight: bold;">$term-&gt;term_id</span> AND p.post_status = 'publish' LIMIT 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">intval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
			<span style="color: #000088;">$filtered_terms</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$term</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$filtered_terms</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'get_terms'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'get_terms_filter'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The code work like this. We add a filter to the <strong>get_terms</strong> hook. This hook is called before the terms is returned, so it is perfect to place the filter here. Next, we check each returned terms and see if they have any published post, by using our own query. Why didn&#8217;t I use the WordPress WP_Query instead? The answer is I can&#8217;t get it to work, beside, it builds a pretty complicated query that might increase your WordPress site load time if it is used too much. Using our own query, we make sure it only use the query that we need. If our query returned that the term has a published post, we add this term to our new array which we use to store the filtered terms. Finally, we return the filtered terms.</p>
<p>Now, when you are using <strong>get_terms</strong>, you will filter out all terms that doesn&#8217;t have any published post. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Hope that helps. I have a hard time to explain the situation, so let me know if it doesn&#8217;t clear.</p>
<p>Thank you.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2010/08/filter-get_terms-to-return-only-terms-with-published-post/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Some useful tips for WordPress custom post type and taxonomy</title>
		<link>http://jeffri.net/2010/07/some-useful-tips-for-wordpress-custom-post-type-and-taxonomy/</link>
		<comments>http://jeffri.net/2010/07/some-useful-tips-for-wordpress-custom-post-type-and-taxonomy/#comments</comments>
		<pubDate>Mon, 19 Jul 2010 23:28:08 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=569</guid>
		<description><![CDATA[Custom post type is one of the most powerful WordPress feature. It allows us to create just any content we need, combined with custom taxonomy, the possibility is just endless. WordPress have allowed to have custom post type and taxonomy for some while, but with WordPress 3, everything is far more easier. We have register_post_type [...]]]></description>
			<content:encoded><![CDATA[<p>Custom post type is one of the most powerful WordPress feature. It allows us to create just any content we need, combined with custom taxonomy, the possibility is just endless. WordPress have allowed to have custom post type and taxonomy for some while, but with WordPress 3, everything is far more easier. We have <a href="http://codex.wordpress.org/Function_Reference/register_post_type">register_post_type</a> and <a href="http://codex.wordpress.org/Function_Reference/register_taxonomy">register_taxonomy</a> created specifically for this purpose.</p>
<p>While this two combined to be a wonderful addition for your WordPress blog, there is still a lot of things that is not yet documented. Here is a few useful tips I collected when working with custom post type and taxonomy.</p>
<p><span id="more-569"></span></p>
<h2>Add taxonomy to more than one post type</h2>
<p>There is few ways to doing this, depend to your code. If you register the post type first, then when register taxonomy, you&#8217;ll be able to include it to any post types you want.</p>
<p>Example (taken from codex).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">  register_taxonomy<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'genre'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'magazine'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'post'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'hierarchical'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'labels'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$labels</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'show_ui'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'query_var'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'rewrite'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'slug'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'genre'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>See the line 1 above, the second parameter for register_taxonomy function is the list of post types you can include. It accept both string and array, as you can expect, we use array when we want to include many post type, and use string when you only need one. The same thing is applied if you register the taxonomy first, you can assign any taxonomy you like when register the post type.</p>
<p>Example (taken from codex)</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'labels'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$labels</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'public'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'publicly_queryable'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'show_ui'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> 
    <span style="color: #0000ff;">'query_var'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'rewrite'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'capability_type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'post'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'hierarchical'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'menu_position'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'taxonomies'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'genre'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'grade'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'category'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'supports'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'editor'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'author'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'thumbnail'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'excerpt'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'comments'</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  register_post_type<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book'</span><span style="color: #339933;">,</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>As you can see, the <em>taxonomies</em> parameter can be used to assign any taxonomy you like to the post type you just registered. Again, it accept both array and string.</p>
<p>But what if you want to add taxonomy to certain post type later, after they are defined? WordPress have a function to do this, it is <a href="http://codex.wordpress.org/Function_Reference/register_taxonomy_for_object_type">register_taxonomy_for_object_type</a>. Take a look on the example below.</p>
<p>Example</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">register_taxonomy_for_object_type<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'genre'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'book'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Handy, huh?</p>
<h2>Querying more than one post type</h2>
<p>Let&#8217;s say, for example, you have defined post type for book, comic and magazine. Then, somewhere in your web page, you want to show the latest list of all of these post types together. As you might know already, custom post type is not included in normal WordPress loop, so you&#8217;ll need to add your own query and loop, whether by using query_posts, get_posts or by creating a new instance of WP_Query object.</p>
<p>Example</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$args</span> <span style="color: #339933;">=</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: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'comic'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'magazine'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'posts_per_page'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
query_posts<span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> the_post<span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// and so on</span>
<span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>See the line 2. You can add any post type you need in the <em>post_type</em> parameter. As always, it also accept both array and string.</p>
<h2>Querying the custom taxonomy</h2>
<p>You know how to querying multiple post type now, but how if you want to filter it by taxonomy? While post category and tags offer many ways to filter it, the custom taxonomy is still limited. The only way I can find right now is to filter the taxonomy by the slug.</p>
<p>Example</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$args</span> <span style="color: #339933;">=</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: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'comic'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'magazine'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'taxonomy'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'genre'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'term'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'science'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'fiction'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'posts_per_page'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
query_posts<span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> the_post<span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// and so on</span>
<span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>From the example above, I filtered the result by including only science and fiction genre from all three post types we defined. Unfortunately, I haven&#8217;t find any way to filter it by multiple taxonomies at once.</p>
<h2>The custom post type feed</h2>
<p>Custom post type also have its own feed. Depend to your permalink structure, you can find it by either</p>
<blockquote><p>http://www.yourwordpresswebsite.com/?feed=rss2&#038;post_type=book</p></blockquote>
<p>or</p>
<blockquote><p>http://www.yourwordpresswebsite.com/feed/?post_type=book</p></blockquote>
<p>Unfortunately, again, I can&#8217;t find any way to get the feed for multiple post type.</p>
<p>So that&#8217;s it for now. If you find any good tips, you can share it in comment. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2010/07/some-useful-tips-for-wordpress-custom-post-type-and-taxonomy/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Blog redesign</title>
		<link>http://jeffri.net/2010/07/blog-redesign/</link>
		<comments>http://jeffri.net/2010/07/blog-redesign/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 01:46:22 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Designing]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[templates]]></category>
		<category><![CDATA[xhtml]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=553</guid>
		<description><![CDATA[Welcome to my new redesigned blog! It&#8217;s been a year and a half since the last time I changed the design. I have always wanted to change it, but I just always don&#8217;t have enough time. I know I&#8217;m not that good enough in designing, but at least, this is the best design I ever [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to my new redesigned blog! It&#8217;s been a year and a half since the last time I changed the design. I have always wanted to change it, but I just always don&#8217;t have enough time. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  I know I&#8217;m not that good enough in designing, but at least, this is the best design I ever made until now!</p>
<p><img src="http://jeffri.net/wp-content/uploads/blog_design.jpg" alt="" title="blog_design" width="560" height="238" class="alignnone size-full wp-image-554" /></p>
<p>The idea behind this design is, to make a simple, usable layout and still looks beautiful. There is a lot of tutorial on the web that helped me when making this. I try to make the website small in size, so you&#8217;ll able to load it fast enough. There is also a cool trick on the background, which still make the markup small without much wrapping div over div to accomplish the effect. I also realize the importance on social networking nowadays, so I add links to some of my account. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><span id="more-553"></span></p>
<p>With CSS 3 coming, I won&#8217;t forget to add some CSS 3 elements. I add a little transition, on the sidebar links and the social network icon above. You won&#8217;t see it if you don&#8217;t use webkit browser though, so if you use Google Chrome 5 and Safari 5, then you are in. This transition have no importance at all, the purpose is just to enrich the user experience, but won&#8217;t lose any functionality without it. Another CSS 3 property I used is the <em>border-radius</em>. I used this on some input fields you can find. There is also some jQuery here, although I&#8217;m only using it on input fields now, it might become handy in future.</p>
<p>While I haven&#8217;t check this on all browser yet, but I&#8217;m sure it will work fine on all major browser, includes Internet Explorer 8, Mozilla Firefox 3.6, Opera 10, Google Chrome 5 and Safari 5. While I&#8217;m not supporting older Internet Explorer version, it still, on some basic level, works fine. I even can confirm that the IE 7 load almost everything fine, but it just not perfect yet. I have some issues on the logo and button, same as IE 6. This might get fixed soon. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I wish I could write the process on making this design. I might be able write it, but I won&#8217;t promise that. In short, there is 2 steps at least. First is design it in PSD, the second is convert it to WordPress. I&#8217;m skipping the step to convert it to XHTML/CSS first though, well, that&#8217;s just how I work. <img src='http://jeffri.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>I hope you will have one or two minute to type down a comment to share your opinion, so I can get better. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Thanks!</p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2010/07/blog-redesign/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Checking the current post type with is_singular function</title>
		<link>http://jeffri.net/2010/07/checking-the-current-post-type-with-is_singular-function/</link>
		<comments>http://jeffri.net/2010/07/checking-the-current-post-type-with-is_singular-function/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 01:38:50 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=541</guid>
		<description><![CDATA[The WordPress 3 introduce a new feature that let us easily make our own custom post type. This is really a cool feature that give more possibility to customize the WordPress powered website. However, I find that the documentation is not complete yet. In the recent problem I got, it requires me to check the [...]]]></description>
			<content:encoded><![CDATA[<p>The WordPress 3 introduce a new feature that let us easily make our own custom post type. This is really a cool feature that give more possibility to customize the WordPress powered website. However, I find that the documentation is not complete yet.</p>
<p>In the recent problem I got, it requires me to check the current post type, something like <a href="http://codex.wordpress.org/Function_Reference/is_single">is_single</a> for post and <a href="http://codex.wordpress.org/Function_Reference/is_page">is_page</a> for page. Luckily enough, the <a href="http://codex.wordpress.org/Function_Reference/is_singular">is_singular</a> is now changed to accept a parameter, and that is post type. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>In current codex page, it still not updated. However, now we can use this to check whether we are on our post type page or not.</p>
<blockquote><p>
if ( is_singular(&#8220;my-post-type&#8221;) )<br />
    // whatever you want to do here
</p></blockquote>
<p>Great!</p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2010/07/checking-the-current-post-type-with-is_singular-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>If the_content stop working after upgrading to WordPress 3</title>
		<link>http://jeffri.net/2010/07/if-the_content-stop-working-after-upgrading-to-wordpress-3/</link>
		<comments>http://jeffri.net/2010/07/if-the_content-stop-working-after-upgrading-to-wordpress-3/#comments</comments>
		<pubDate>Sun, 04 Jul 2010 06:38:37 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=537</guid>
		<description><![CDATA[Well, this is one common problem happened after upgrading to WordPress 3. After upgrading to WordPress 3, the content is not displayed. This is happened likely because you don&#8217;t call the_post before you call the_content. In previous version of WordPress, when we are on single/page, this worked fine. Some themes didn&#8217;t go into the WordPress [...]]]></description>
			<content:encoded><![CDATA[<p>Well, this is one common problem happened after upgrading to WordPress 3. After upgrading to WordPress 3, the content is not displayed. This is happened likely because you don&#8217;t call <em>the_post</em> before you call <em>the_content</em>.</p>
<p>In previous version of WordPress, when we are on single/page, this worked fine. Some themes didn&#8217;t go into the WordPress loop on single/page template, since WordPress already know which page/post to show. This also make sense, single/page only have 1 post to be displayed, so we shouldn&#8217;t need to get into the WordPress loop. However, in the recent version, <em>the_content</em> failed to display anything if you don&#8217;t call <em>the_post</em> beforehand.</p>
<p>I&#8217;m not sure which one should be the correct behavior. In <a href="http://codex.wordpress.org/Pages#Page_Templates">codex</a>, it&#8217;s pretty clear that we still need to get into WordPress loop, even though we are in single/page template. However, in theory, calling <em>the_post</em> before calling any other function that supposed to be in <a href="http://codex.wordpress.org/The_Loop">the loop</a> should fix it.</p>
<p>Just a quick note, I don&#8217;t want to forgot this trivial stuff when I got this problem again in future. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2010/07/if-the_content-stop-working-after-upgrading-to-wordpress-3/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Frustating WordPress Database Error</title>
		<link>http://jeffri.net/2010/06/frustating-wordpress-database-error/</link>
		<comments>http://jeffri.net/2010/06/frustating-wordpress-database-error/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 09:27:08 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[install]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=472</guid>
		<description><![CDATA[Have you ever got this error? While this happens, you must make sure you have configured your wp-config.php. Check for these line: define(&#8216;DB_NAME&#8217;, &#8216;putyourdbnamehere&#8217;); /** MySQL database username */ define(&#8216;DB_USER&#8217;, &#8216;usernamehere&#8217;); /** MySQL database password */ define(&#8216;DB_PASSWORD&#8217;, &#8216;yourpasswordhere&#8217;); /** MySQL hostname */ define(&#8216;DB_HOST&#8217;, &#8216;localhost&#8217;); Above is taken from wp-config-sample.php file. As long as you have [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://jeffri.net/wp-content/uploads/wp_error.jpg" alt="" title="wp_error" width="540" height="72" class="alignnone size-full wp-image-473" /></p>
<p>Have you ever got this error? While this happens, you must make sure you have configured your wp-config.php. Check for these line:</p>
<blockquote><p>
define(&#8216;DB_NAME&#8217;, &#8216;putyourdbnamehere&#8217;);</p>
<p>/** MySQL database username */<br />
define(&#8216;DB_USER&#8217;, &#8216;usernamehere&#8217;);</p>
<p>/** MySQL database password */<br />
define(&#8216;DB_PASSWORD&#8217;, &#8216;yourpasswordhere&#8217;);</p>
<p>/** MySQL hostname */<br />
define(&#8216;DB_HOST&#8217;, &#8216;localhost&#8217;);
</p></blockquote>
<p>Above is taken from wp-config-sample.php file. As long as you have configured the DB_NAME, DB_USER and DB_PASSWORD correctly, you should be fine. Some host might use a different DB_HOST, but mostly worked fine with localhost.</p>
<p>Well, if it does configured correctly and there still the same error happen, you might need to check the wp_options table. </p>
<p>Open the wp_options table using PHPMyAdmin or other similar software. Find the value <em>siteurl</em> in column <em>option_name</em>, if you see the <em>option_value</em> get a blank value like below:</p>
<p><img src="http://jeffri.net/wp-content/uploads/wp_options.jpg" alt="" title="wp_options" width="540" height="51" class="alignnone size-full wp-image-474" /></p>
<p>Then you are probably on right track. Edit this row and insert your domain full URL in the <em>option_value</em>. Save.</p>
<p>Hopefully the error will go away. I don&#8217;t know why this could happen, but it did happen in one of my client website&#8230; <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  It&#8217;s indeed frustating to look for this error, since WordPress didn&#8217;t give any specific information about it.</p>
<p>If you found another cause of this error, please share.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2010/06/frustating-wordpress-database-error/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

