<?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>Bad Deacon Design &#187; Code Snippets</title>
	<atom:link href="http://baddeacondesign.com/blog/category/code-snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://baddeacondesign.com</link>
	<description>Woodblock Prints, Printmaking, and Fine Art by Sean &#34;Deacon&#34; Neprud</description>
	<lastBuildDate>Tue, 20 Jul 2010 19:45:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Wordpress: How to scrape the Tags from Posts of a certain Category</title>
		<link>http://baddeacondesign.com/blog/2009/05/wordpress-how-to-scrape-tags/</link>
		<comments>http://baddeacondesign.com/blog/2009/05/wordpress-how-to-scrape-tags/#comments</comments>
		<pubDate>Fri, 08 May 2009 16:57:33 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tags]]></category>

		<guid isPermaLink="false">http://baddeacondesign.com/?p=31</guid>
		<description><![CDATA[
			
				
			
		
One of the features of Wordpress is that it has a 2-D taxonomy built into it (taxonomy refers to the method of categorizing information).
The built in taxonomy consists of Categories and Tags.
For this site, which I am working on redesigning, I want to separate large portions of this site by category. For instance, I want [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="background-image:url('/img/buttonbird-bigger.png'); background-repeat: no-repeat; height: 79; width: 122px; float: right; margin-left: 10px; padding-left: 6px; padding-top: 6px; padding-bottom: 6px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fbaddeacondesign.com%2Fblog%2F2009%2F05%2Fwordpress-how-to-scrape-tags%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fbaddeacondesign.com%2Fblog%2F2009%2F05%2Fwordpress-how-to-scrape-tags%2F&amp;source=baddeacon&amp;style=normal&amp;alias=&amp;space=20" height="61" width="50" /><br />
			</a>
		</div>
<p>One of the features of Wordpress is that it has a 2-D taxonomy built into it (taxonomy refers to the method of categorizing information).</p>
<p>The built in taxonomy consists of Categories and Tags.</p>
<p>For this site, which I am working on redesigning, I want to separate large portions of this site by category. For instance, I want a portion of the site to be information and articles focusing on Web Design and Development. I want another to be my personal blog. I may add others down the road, like a section displaying my art, and so on.</p>
<p>My plan was to distinguish these areas by assigning them different categories within Wordpress.</p>
<p>This would only leave me to use Tags to further categorize content within these major areas.</p>
<p>In this way, I am really using Tags like Categories. Sure, this leaves me without the functionality of tags, but really, I don&#8217;t care.</p>
<p>So here&#8217;s the problem:</p>
<p>Wordpress doesn&#8217;t really treat Tags and Categories as a true 2-Dimensional taxonomy system. The way Wordpress functions, it is more like a 1-Dimensional system with two segments, and slightly different functions are built in to each.</p>
<p>I wanted to be able to pull a list of all the tags assigned to posts of a certain category, but this proved hard to do, or hard to find out how to do, with Wordpress&#8217;s current functions and documentation.</p>
<p>I found a piece of code that did something similar as a starting point, and modified it to make it do what I needed it to do.</p>
<p>This code determines which posts are in a specified category or categories, determines the id numbers of the tags associated with each post, throws out duplicate tag ids, then lists all of the tags with those id numbers in an unordered list.</p>
<p>Here&#8217;s the code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;div class=&quot;tag&quot;&gt;
&lt;!-- This div isn't needed if you don't want it --&gt;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$category_id</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//This creates an array of the category ids you wanna </span>
<span style="color: #666666; font-style: italic;">//scrape the tags for. By making it an array, multiple </span>
<span style="color: #666666; font-style: italic;">//categories can be scraped.  Replace &quot;3&quot; above with the </span>
<span style="color: #666666; font-style: italic;">//category ID number of whatever category or categories </span>
<span style="color: #666666; font-style: italic;">//you wanna scrape.  Separate multiple category IDs with </span>
<span style="color: #666666; font-style: italic;">//commas.</span>
&nbsp;
<span style="color: #000088;">$post_ids</span> <span style="color: #339933;">=</span> get_objects_in_term<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$category_id</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: #666666; font-style: italic;">//This assigns an array of the id numbers of posts in the </span>
<span style="color: #666666; font-style: italic;">//defined category(s)</span>
&nbsp;
<span style="color: #000088;">$tag_ids_raw</span> <span style="color: #339933;">=</span> wp_get_object_terms<span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$post_ids</span><span style="color: #339933;">,</span> 
<span style="color: #0000ff;">'post_tag'</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;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'ids'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// This assigns an array of the id numbers of tags for the </span>
<span style="color: #666666; font-style: italic;">//post numbers assigned to $post_ids</span>
<span style="color: #000088;">$tag_ids</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_unique</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tag_ids_raw</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//This strips away duplicate tag ids in the array</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tag_ids</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
&lt;ul&gt;
<span style="color: #000000; font-weight: bold;">&lt;?PHP</span> <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tag_ids</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$mytagid</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #000088;">$tagarray</span><span style="color: #339933;">=</span>get_tag<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mytagid</span><span style="color: #339933;">,</span> ARRAY_N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//In the resulting array, of $tagarray, [0]=&amp;gt;tag </span>
<span style="color: #666666; font-style: italic;">//id, [1]=&amp;gt;tag name, [2]=&amp;gt;tag slug.  Not sure </span>
<span style="color: #666666; font-style: italic;">//what [3] through [9] are yet.</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;li&gt;
&lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> get_tag_link<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mytagid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tagarray</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/a&gt;
&lt;/li&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> &lt;!-- end FOREACH Loop --&gt;
&lt;/ul&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> &lt;!-- end IF Statement --&gt;
&lt;/div&gt; &lt;!-- end div class=&quot;tag&quot; --&gt;</pre></div></div>

<p>EDIT: I moved the unordered list html tags to within the IF statement, so that you don&#8217;t end up with an empty unordered list.</p>
<p>Shortcomings that I know of: The list is not ordered by anything except internal Wordpress database IDs, so it appears to be in random order. Also, I don&#8217;t know how this works with child categories.</p>
<p>If you know of a better way to do this, let me know!</p>

<div class="sociable">
<div class="sociable_tagline">
<small>Share and Enjoy:</small>
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fbaddeacondesign.com%2Fblog%2F2009%2F05%2Fwordpress-how-to-scrape-tags%2F&amp;title=Wordpress%3A%20How%20to%20scrape%20the%20Tags%20from%20Posts%20of%20a%20certain%20Category&amp;bodytext=One%20of%20the%20features%20of%20Wordpress%20is%20that%20it%20has%20a%202-D%20taxonomy%20built%20into%20it%20%28taxonomy%20refers%20to%20the%20method%20of%20categorizing%20information%29.%0D%0A%0D%0AThe%20built%20in%20taxonomy%20consists%20of%20Categories%20and%20Tags.%0D%0A%0D%0AFor%20this%20site%2C%20which%20I%20am%20working%20on%20redesigning%2C%20I" title="Digg"><img src="http://baddeacondesign.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fbaddeacondesign.com%2Fblog%2F2009%2F05%2Fwordpress-how-to-scrape-tags%2F&amp;title=Wordpress%3A%20How%20to%20scrape%20the%20Tags%20from%20Posts%20of%20a%20certain%20Category" title="StumbleUpon"><img src="http://baddeacondesign.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fbaddeacondesign.com%2Fblog%2F2009%2F05%2Fwordpress-how-to-scrape-tags%2F&amp;title=Wordpress%3A%20How%20to%20scrape%20the%20Tags%20from%20Posts%20of%20a%20certain%20Category&amp;notes=One%20of%20the%20features%20of%20Wordpress%20is%20that%20it%20has%20a%202-D%20taxonomy%20built%20into%20it%20%28taxonomy%20refers%20to%20the%20method%20of%20categorizing%20information%29.%0D%0A%0D%0AThe%20built%20in%20taxonomy%20consists%20of%20Categories%20and%20Tags.%0D%0A%0D%0AFor%20this%20site%2C%20which%20I%20am%20working%20on%20redesigning%2C%20I" title="del.icio.us"><img src="http://baddeacondesign.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.mixx.com/submit?page_url=http%3A%2F%2Fbaddeacondesign.com%2Fblog%2F2009%2F05%2Fwordpress-how-to-scrape-tags%2F&amp;title=Wordpress%3A%20How%20to%20scrape%20the%20Tags%20from%20Posts%20of%20a%20certain%20Category" title="Mixx"><img src="http://baddeacondesign.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fbaddeacondesign.com%2Fblog%2F2009%2F05%2Fwordpress-how-to-scrape-tags%2F&amp;t=Wordpress%3A%20How%20to%20scrape%20the%20Tags%20from%20Posts%20of%20a%20certain%20Category" title="Facebook"><img src="http://baddeacondesign.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fbaddeacondesign.com%2Fblog%2F2009%2F05%2Fwordpress-how-to-scrape-tags%2F&amp;title=Wordpress%3A%20How%20to%20scrape%20the%20Tags%20from%20Posts%20of%20a%20certain%20Category&amp;source=Bad+Deacon+Design+Woodblock+Prints%2C+Printmaking%2C+and+Fine+Art+by+Sean+%26quot%3BDeacon%26quot%3B+Neprud&amp;summary=One%20of%20the%20features%20of%20Wordpress%20is%20that%20it%20has%20a%202-D%20taxonomy%20built%20into%20it%20%28taxonomy%20refers%20to%20the%20method%20of%20categorizing%20information%29.%0D%0A%0D%0AThe%20built%20in%20taxonomy%20consists%20of%20Categories%20and%20Tags.%0D%0A%0D%0AFor%20this%20site%2C%20which%20I%20am%20working%20on%20redesigning%2C%20I" title="LinkedIn"><img src="http://baddeacondesign.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="" title="TwitThis"><img src="http://baddeacondesign.com/wp-content/plugins/sociable/images/" title="TwitThis" alt="TwitThis" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fbaddeacondesign.com%2Fblog%2F2009%2F05%2Fwordpress-how-to-scrape-tags%2F&amp;title=Wordpress%3A%20How%20to%20scrape%20the%20Tags%20from%20Posts%20of%20a%20certain%20Category&amp;annotation=One%20of%20the%20features%20of%20Wordpress%20is%20that%20it%20has%20a%202-D%20taxonomy%20built%20into%20it%20%28taxonomy%20refers%20to%20the%20method%20of%20categorizing%20information%29.%0D%0A%0D%0AThe%20built%20in%20taxonomy%20consists%20of%20Categories%20and%20Tags.%0D%0A%0D%0AFor%20this%20site%2C%20which%20I%20am%20working%20on%20redesigning%2C%20I" title="Google Bookmarks"><img src="http://baddeacondesign.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://baddeacondesign.com/blog/2009/05/wordpress-how-to-scrape-tags/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
