<?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>the Scrap book of David Jon</title>
	<atom:link href="http://www.davidjon.nl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.davidjon.nl</link>
	<description>A blog by a Multimedia addict.</description>
	<lastBuildDate>Wed, 29 Jun 2011 17:51:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Only show posts with attachments in a loop</title>
		<link>http://www.davidjon.nl/only-show-posts-with-attachments-in-a-loop/</link>
		<comments>http://www.davidjon.nl/only-show-posts-with-attachments-in-a-loop/#comments</comments>
		<pubDate>Wed, 29 Jun 2011 16:41:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wpquery]]></category>

		<guid isPermaLink="false">http://www.davidjon.nl/blog/?p=4</guid>
		<description><![CDATA[Last night when I was working on a redesign of one of my clients website. The idea was to show a thumbnail and title for 4 random posts. Easy enough I thought. Just wp_Query that stuff and in the loop &#8230; <a href="http://www.davidjon.nl/only-show-posts-with-attachments-in-a-loop/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><strong>Last night when I was working on a redesign of one of my clients website. The idea was to show a thumbnail and title for 4 random posts. Easy enough I thought. Just <em>wp_Query</em> that stuff and in the loop but the code to retrieve the image. However, that didn&#8217;t work the way I wanted. Since some of the posts don&#8217;t have an image. </strong></p>
<p>Therefore I had to come up with a different solution. After googling around I came across this forum topic. <a href="http://wordpress.org/support/topic/exclude-posts-without-attachments-from-query">http://wordpress.org/support/topic/exclude-posts-without-attachments-from-query</a></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$posts</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 *
  FROM <span style="color: #006699; font-weight: bold;">$wpdb-&gt;posts</span>
  WHERE
      post_status = 'publish'
    AND
      ID IN (
	SELECT DISTINCT post_parent
	FROM <span style="color: #006699; font-weight: bold;">$wpdb-&gt;posts</span>
	WHERE
	  post_parent &gt; 0
	AND
	  post_type = 'attachment'
	AND
	  post_mime_type IN ('image/jpeg', 'image/png')
      )
    ORDER BY post_date DESC
&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$posts</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>
  setup_postdata<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* POST THE CODE FOR THE LOOP HERE */</span>
<span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span>
wp_reset_query<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Showing me how to use <strong>MySQL </strong>to only loop posts with an image. And leave out the the ones that don&#8217;t contain an attachment. It worked like a charm. However I had to make a couple of alterations to the code, since I wanted to show posts only from a specific category. And because the WordPress Database uses a different table for posts and categories a had to find a way to link the tables. Since my knowledge of MySQL is very poor, I had to head back to google again. After a little search I came across this link.</p>
<p><a href="http://wordpress.org/support/topic/mysql-query-for-all-posts-in-news-category-id-problem">http://wordpress.org/support/topic/mysql-query-for-all-posts-in-news-category-id-problem</a></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">SELECT <span style="color: #339933;">*</span> FROM wp_posts p
LEFT OUTER <span style="color: #990000;">JOIN</span> wp_term_relationships r ON r<span style="color: #339933;">.</span>object_id <span style="color: #339933;">=</span> p<span style="color: #339933;">.</span>ID
LEFT OUTER <span style="color: #990000;">JOIN</span> wp_term_taxonomy x ON x<span style="color: #339933;">.</span>term_taxonomy_id <span style="color: #339933;">=</span> r<span style="color: #339933;">.</span>term_taxonomy_id
LEFT OUTER <span style="color: #990000;">JOIN</span> wp_terms t ON t<span style="color: #339933;">.</span>term_id <span style="color: #339933;">=</span> x<span style="color: #339933;">.</span>term_id
WHERE p<span style="color: #339933;">.</span>post_status <span style="color: #339933;">=</span> <span style="color: #0000ff;">'publish'</span>
AND p<span style="color: #339933;">.</span>post_type <span style="color: #339933;">=</span> <span style="color: #0000ff;">'post'</span>
AND t<span style="color: #339933;">.</span>slug <span style="color: #339933;">=</span> <span style="color: #0000ff;">'news'</span></pre></div></div>

<p>Showing me how to link the tables. So now the only thing I had to do is combine everything and put in a php variable for post category.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$posts</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 *
 FROM <span style="color: #006699; font-weight: bold;">$wpdb-&gt;posts</span> p
 LEFT OUTER JOIN <span style="color: #006699; font-weight: bold;">$wpdb-&gt;term_relationships</span> r ON r.object_id = p.ID
 LEFT OUTER JOIN <span style="color: #006699; font-weight: bold;">$wpdb-&gt;term_taxonomy</span> x ON x.term_taxonomy_id = r.term_taxonomy_id
 LEFT OUTER JOIN <span style="color: #006699; font-weight: bold;">$wpdb-&gt;terms</span> t ON t.term_id = x.term_id
  WHERE
      p.post_status = 'publish'
    AND
	x.parent = '7'          /* Only show posts in this category or it's children */
    AND
      ID IN (
	SELECT DISTINCT p.post_parent
	FROM <span style="color: #006699; font-weight: bold;">$wpdb-&gt;posts</span> p
	WHERE
	  p.post_parent &gt; 0
	AND
	  p.post_type = 'attachment'
	AND
	  p. post_mime_type IN ('image/jpeg', 'image/png')
      )
&nbsp;
    ORDER BY rand() DESC     /* Order the posts randomly */
	LIMIT 0,4      /* Only get 4 posts */
&nbsp;
&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$posts</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>
  setup_postdata<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
         <span style="color: #666666; font-style: italic;">/* PLACE THE CODE FOR YOUR LOOP HERE */</span>
<span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span>
wp_reset_query<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Now it searches through the database, checking if a post is published, if it&#8217;s parents category is number 7 (you can change this to whatever fits your needs) and if the post has an jpeg or png attached to it. <br />Then it displays them randomly and limits it to 4 posts. You can change that in whatever you need.</p>
<p><em>Please note : </em>I used <code>x.parent = '7'</code> because I wanted to get the posts in all subcategories of category 7. You can also use <code>t.term_id = '7'</code> if you only need to display the posts in that particular category, without looking at it&#8217;s subcategories.</p>
<p>I also wanted to use this code to show 4 random posts in the same category as the current post in the single template but excluding the current post. After finding all of the above, this was a breeze. All i had to do is create a variable to retrieve the current post ID and current category ID. That total piece of code looks like this.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/* GET the current category ID */</span>
<span style="color: #000088;">$category</span> <span style="color: #339933;">=</span> get_the_category<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$category_id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$category</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cat_ID</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* GET the current Post ID */</span>
<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wp_query</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$thePostID</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wp_query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$posts</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 *
 FROM  <span style="color: #006699; font-weight: bold;">$wpdb-&gt;posts</span> p
 LEFT OUTER JOIN  <span style="color: #006699; font-weight: bold;">$wpdb-&gt;term_relationships</span> r ON r.object_id = p.ID
 LEFT OUTER JOIN  <span style="color: #006699; font-weight: bold;">$wpdb-&gt;term_taxonomy</span> x ON x.term_taxonomy_id = r.term_taxonomy_id
 LEFT OUTER JOIN  <span style="color: #006699; font-weight: bold;">$wpdb-&gt;terms</span> t ON t.term_id = x.term_id
  WHERE
      p.post_status = 'publish'
    AND
	t.term_id = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$category_id</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;' /* Only show posts if in current category */
    AND
	p.ID != '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$thePostID</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;' /* Don't show the current post */
    AND
      ID IN (
	SELECT DISTINCT p.post_parent
	FROM  <span style="color: #006699; font-weight: bold;">$wpdb-&gt;posts</span> p
	WHERE
	  p.post_parent &gt; 0
	AND
	  p.post_type = 'attachment'
	AND
	  p. post_mime_type IN ('image/jpeg', 'image/png')
      )
&nbsp;
    ORDER BY rand() DESC      /* Order the posts randomly */
	LIMIT 0,4    /* Only get 4 posts */
&nbsp;
&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$posts</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>
  setup_postdata<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* PLACE THE CODE FOR YOU LOOP HERE */</span>
&nbsp;
<span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span>
wp_reset_query<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This might not be a very clean solution. But it works like a charm, so I&#8217;m happy. Hopefully the WordPress Developers will find a way to incorporate something like this in a future version of WordPress. </p>
<p>I&#8217;m not really a developer, I just like to find a solution to the problems I encounter when working with WordPress. So if anyone knows a better solution please share it in the comment section below. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.davidjon.nl/only-show-posts-with-attachments-in-a-loop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

