[wp-pro] $10 via Paypal to Answer This Question

Casey Bisson casey.bisson at gmail.com
Mon Jun 14 04:03:28 UTC 2010


Two ways, both have their drawbacks:

You'll probably do best by running multiple loops. That way you know exactly where each category of posts begins and ends and you can choose how many you want in each section. You'll also be able to avoid burying a section because you had a run on another section and your most recent n posts are all the same category. 

On the other hand, maybe that's what you want, or maybe you have other ideas. In that case...

MySQL's ORDER BY FIELD($fieldname, ...list of matching entries in the order you want them...) syntax my be what you need. You'd have to join the term relationships table, then order by term_taxonomy_id (or go all the way and join the other tables to order by category name)

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_field

If you go that route, the posts_join and posts_orderby filters will be your friends. You'd probably want to hook a function to the pre_get_posts action that checks that the request is for the front page and then applies the filters. You should probably also apply a hook the to remove the filters on loop_end or something like that so they don't disrupt other queries. 

--Casey Bisson

http://MaisonBisson.com
http://Scriblio.net


  SELECT * FROM wp_posts WHERE ID IN(5,10,15,20,25,30) ORDER BY FIELD(ID, 10,30,20,25,5,15)    Probably not efficient for very large result sets, but...  On Mon, Feb 15, 2010 at 7:40 PM, Andy Skelton <skeltoac at gmail.com> wrote:  > The second example uses a subquery to find the ID's. Subqueries rock. > You can use them to get the set of ID's from other tables in your > database. But this example has an important limitation: you can't > control the order of the final result set by the order of the subquery > in an IN clause. > 



On Jun 13, 2010, at 6:37 PM, Alex Bluesummers <MrBluesummers at MrBluesummers.com> wrote:

> First person with an elegant answer gets it!
> 
> Question
> How do I order posts in the loop by whether or not it is in a category, then by date?
> 
> Context
> Suppose I have 10 posts, of which 5 are in the category "Sports" and 5 are in the category "Blog News".  Both "Sports" and "Blog News" posts are mixed together chronologically.  "Sports" and "Blog News" posts share other categories and tags.  I want both types of posts to be present in the loop regardless of whether it's the front page or category archive view, but ordered by "Sports" and "blog news" and then by date.  How is this most elegantly accomplished?
> 
> Alex Bluesummers | http://www.MrBluesummers.com
> _______________________________________________
> wp-pro mailing list
> wp-pro at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-pro
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.automattic.com/pipermail/wp-pro/attachments/20100614/0f4cec00/attachment.htm>


More information about the wp-pro mailing list