<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: WordPress: Sort Posts by Multiple Fields</title>
	<atom:link href="http://jeffgran.com/218/blog/wordpress-sort-posts-by-multiple-fields/feed" rel="self" type="application/rss+xml" />
	<link>http://jeffgran.com/218/blog/wordpress-sort-posts-by-multiple-fields</link>
	<description>Artist, Designer, Developer</description>
	<lastBuildDate>Mon, 28 Nov 2011 21:53:51 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Hade</title>
		<link>http://jeffgran.com/218/blog/wordpress-sort-posts-by-multiple-fields/comment-page-1#comment-587</link>
		<dc:creator>Hade</dc:creator>
		<pubDate>Tue, 30 Aug 2011 08:43:17 +0000</pubDate>
		<guid isPermaLink="false">http://jeffgran.com/?p=218#comment-587</guid>
		<description>Fantastic, I was looking for a way to order by multiple criteria everywhere.
Thank you!</description>
		<content:encoded><![CDATA[<p>Fantastic, I was looking for a way to order by multiple criteria everywhere.<br />
Thank you!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff Gran</title>
		<link>http://jeffgran.com/218/blog/wordpress-sort-posts-by-multiple-fields/comment-page-1#comment-335</link>
		<dc:creator>Jeff Gran</dc:creator>
		<pubDate>Thu, 14 Jul 2011 02:22:16 +0000</pubDate>
		<guid isPermaLink="false">http://jeffgran.com/?p=218#comment-335</guid>
		<description>@Steve,

Basically, you query the posts via query_posts first. Wordpress has a global variable called $wp_query, which gets populated when you run query_posts(). 

So then when you run the usort on those posts, it sorts them. It&#039;s inefficient since it&#039;s getting all the posts from MySQL and then sorting them all in memory (which if you have a ton of posts would be veeery slow). But it works OK if you have a small number of posts.

In the example above, I am sorting by stars, and THEN within each block of the same number of stars, sorting by priority. You should be able to take the example tie-breaker function and adapt it to your needs.</description>
		<content:encoded><![CDATA[<p>@Steve,</p>
<p>Basically, you query the posts via query_posts first. WordPress has a global variable called $wp_query, which gets populated when you run query_posts(). </p>
<p>So then when you run the usort on those posts, it sorts them. It&#8217;s inefficient since it&#8217;s getting all the posts from MySQL and then sorting them all in memory (which if you have a ton of posts would be veeery slow). But it works OK if you have a small number of posts.</p>
<p>In the example above, I am sorting by stars, and THEN within each block of the same number of stars, sorting by priority. You should be able to take the example tie-breaker function and adapt it to your needs.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://jeffgran.com/218/blog/wordpress-sort-posts-by-multiple-fields/comment-page-1#comment-334</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Fri, 01 Jul 2011 20:24:37 +0000</pubDate>
		<guid isPermaLink="false">http://jeffgran.com/?p=218#comment-334</guid>
		<description>&quot; For example, if you wanted to order your posts by author, but for each author make sure they are in chronological order.&quot;

Can you give an example of how this might work? In my case, I want to sort first by a custom field, and then alphabetically by post title within each &quot;block&quot; of posts of the custom field. I&#039;m also not clear how the usort line is meant to integrate with a standard query_posts.

Thanks!</description>
		<content:encoded><![CDATA[<p>&#8221; For example, if you wanted to order your posts by author, but for each author make sure they are in chronological order.&#8221;</p>
<p>Can you give an example of how this might work? In my case, I want to sort first by a custom field, and then alphabetically by post title within each &#8220;block&#8221; of posts of the custom field. I&#8217;m also not clear how the usort line is meant to integrate with a standard query_posts.</p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: daniel mamann puerto vallarta</title>
		<link>http://jeffgran.com/218/blog/wordpress-sort-posts-by-multiple-fields/comment-page-1#comment-333</link>
		<dc:creator>daniel mamann puerto vallarta</dc:creator>
		<pubDate>Sun, 29 May 2011 03:06:28 +0000</pubDate>
		<guid isPermaLink="false">http://jeffgran.com/?p=218#comment-333</guid>
		<description>Hey there! I know this is kinda off topic but I&#039;d figured I&#039;d ask. Would you be interested in exchanging links or maybe guest authoring a blog post or vice-versa? My site addresses a lot of the same topics as yours and I think we could greatly benefit from each other. If you happen to be interested feel free to shoot me an e-mail. I look forward to hearing from you! Awesome blog by the way!</description>
		<content:encoded><![CDATA[<p>Hey there! I know this is kinda off topic but I&#8217;d figured I&#8217;d ask. Would you be interested in exchanging links or maybe guest authoring a blog post or vice-versa? My site addresses a lot of the same topics as yours and I think we could greatly benefit from each other. If you happen to be interested feel free to shoot me an e-mail. I look forward to hearing from you! Awesome blog by the way!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: elite destinations reviews</title>
		<link>http://jeffgran.com/218/blog/wordpress-sort-posts-by-multiple-fields/comment-page-1#comment-332</link>
		<dc:creator>elite destinations reviews</dc:creator>
		<pubDate>Sat, 28 May 2011 03:45:05 +0000</pubDate>
		<guid isPermaLink="false">http://jeffgran.com/?p=218#comment-332</guid>
		<description>I am curious to find out what blog platform you have been working with? I&#039;m having some small security problems with my latest website and I&#039;d like to find something more secure. Do you have any recommendations?</description>
		<content:encoded><![CDATA[<p>I am curious to find out what blog platform you have been working with? I&#8217;m having some small security problems with my latest website and I&#8217;d like to find something more secure. Do you have any recommendations?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Elizabeth</title>
		<link>http://jeffgran.com/218/blog/wordpress-sort-posts-by-multiple-fields/comment-page-1#comment-328</link>
		<dc:creator>Elizabeth</dc:creator>
		<pubDate>Wed, 23 Mar 2011 15:19:35 +0000</pubDate>
		<guid isPermaLink="false">http://jeffgran.com/?p=218#comment-328</guid>
		<description>I need to query posts so that they sort first by date with the oldest appearing first, then by category with the lowest number appearing first. I can&#039;t get anything to work. Please help!

</description>
		<content:encoded><![CDATA[<p>I need to query posts so that they sort first by date with the oldest appearing first, then by category with the lowest number appearing first. I can&#8217;t get anything to work. Please help!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff Gran</title>
		<link>http://jeffgran.com/218/blog/wordpress-sort-posts-by-multiple-fields/comment-page-1#comment-321</link>
		<dc:creator>Jeff Gran</dc:creator>
		<pubDate>Sat, 27 Nov 2010 05:52:53 +0000</pubDate>
		<guid isPermaLink="false">http://jeffgran.com/?p=218#comment-321</guid>
		<description>@Will: yes, that&#039;s exactly what the above code is for. It allows you to sort on one field, and then sort each grouping within that first sort, just like you&#039;re talking about. 

@chety: In the instance above, it would be a bit more complicated than just a simple hook into the posts_orderby hook, because there are also joins involved, so you&#039;d also have to add the join into the join hook.  But basically the idea is to use SQL to do the sorting for you before it returns the posts, instead of getting ALL the posts and then sorting them in memory, via the PHP code. 

But if you were just ordering on two different standard fields (meaning, two fields both in the wp_posts table),  in your SQL code the order clause would look something like &#039;order by post_title, post_date&#039;.  The first column is the first one to sort on, and the second one will sort within the first sort.</description>
		<content:encoded><![CDATA[<p>@Will: yes, that&#8217;s exactly what the above code is for. It allows you to sort on one field, and then sort each grouping within that first sort, just like you&#8217;re talking about. </p>
<p>@chety: In the instance above, it would be a bit more complicated than just a simple hook into the posts_orderby hook, because there are also joins involved, so you&#8217;d also have to add the join into the join hook.  But basically the idea is to use SQL to do the sorting for you before it returns the posts, instead of getting ALL the posts and then sorting them in memory, via the PHP code. </p>
<p>But if you were just ordering on two different standard fields (meaning, two fields both in the wp_posts table),  in your SQL code the order clause would look something like &#8216;order by post_title, post_date&#8217;.  The first column is the first one to sort on, and the second one will sort within the first sort.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: chety</title>
		<link>http://jeffgran.com/218/blog/wordpress-sort-posts-by-multiple-fields/comment-page-1#comment-319</link>
		<dc:creator>chety</dc:creator>
		<pubDate>Thu, 18 Nov 2010 14:37:32 +0000</pubDate>
		<guid isPermaLink="false">http://jeffgran.com/?p=218#comment-319</guid>
		<description>Can you show how to use posts_orderby to sort by multiple fields?</description>
		<content:encoded><![CDATA[<p>Can you show how to use posts_orderby to sort by multiple fields?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Will</title>
		<link>http://jeffgran.com/218/blog/wordpress-sort-posts-by-multiple-fields/comment-page-1#comment-318</link>
		<dc:creator>Will</dc:creator>
		<pubDate>Wed, 17 Nov 2010 18:31:38 +0000</pubDate>
		<guid isPermaLink="false">http://jeffgran.com/?p=218#comment-318</guid>
		<description>I have a site that needs to sort by date first, then by a custom value (sort_index).

E.g.   Group A has 5 news stories from 11/17.    Group B has 5 news stories from the day before (11/16).

Now, on the main archive page, the visitor would see these sorted by date descending.

But within each day, the posts would be sorted by a custom (sort_index) value.

Using the above method, im seeing the entire post list resorted according to this sort_index value.   that is, it doesn&#039;t seem to be double sorting but rather *re-sorting*.

Does this make sense?   Am I way off base here?  :P

Cheers, and thanks Jeff.</description>
		<content:encoded><![CDATA[<p>I have a site that needs to sort by date first, then by a custom value (sort_index).</p>
<p>E.g.   Group A has 5 news stories from 11/17.    Group B has 5 news stories from the day before (11/16).</p>
<p>Now, on the main archive page, the visitor would see these sorted by date descending.</p>
<p>But within each day, the posts would be sorted by a custom (sort_index) value.</p>
<p>Using the above method, im seeing the entire post list resorted according to this sort_index value.   that is, it doesn&#8217;t seem to be double sorting but rather *re-sorting*.</p>
<p>Does this make sense?   Am I way off base here?  <img src='http://jeffgran.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>Cheers, and thanks Jeff.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marget Michelotti</title>
		<link>http://jeffgran.com/218/blog/wordpress-sort-posts-by-multiple-fields/comment-page-1#comment-311</link>
		<dc:creator>Marget Michelotti</dc:creator>
		<pubDate>Sat, 07 Aug 2010 00:30:31 +0000</pubDate>
		<guid isPermaLink="false">http://jeffgran.com/?p=218#comment-311</guid>
		<description>Awesome job!</description>
		<content:encoded><![CDATA[<p>Awesome job!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

