Sunday 15 August 2010

php - How to order by two different things at once in a query in WordPress -


I'm having a problem I'm trying to get the latest 12 posts through custom meta fields. Out of those 12 posts, I want to order them from the date of the post. So first I pull 12 posts using custom meta fields and order them to find the latest through the Meta field. Once I have, I want to order them again with the latest post.

This is my current code, I do not know how I can keep two order-bills in a query ...

  $ Recent Episodes 12 = New WP_Query (Array ('Posts_per_page' = & gt; 12, 'Post_type' = & gt; 'Post', 'Meta_Key' = & gt; 'AirDate', 'Order' => 'DESC', 'Orderbie' = & Array ('key' = & gt; 'air_date',), array ('key' = 'gt;' already fixed ',' value ',' meta_view_num ',' meta_query '= & gt; array '= & Gt;' yes', 'compare' = & gt; '=')),));    

, you have to separate them from one location:

Multiple 'orderbird' values ​​

Show pages sorted by 'title' and 'menu_order' (Title is effective):

  $ query = new WP_Query (array ('post_type' = & gt; 'page', 'orderbie' = & gt; 'title menu_order', 'order' = & Gt; 'ASS'));    

In your case, it will look like this:

  'orderby' = & gt; 'Meta_value_num date'   

Edit: OK, it seems that you are trying to do a bit more complex work here. according to <12> air_date only 12 latest items.

  • Order date by 12 items.

    What orderby originally:

    1. Order by air_date .
    2. If an item contains same air_date values, order them from date .
    3. Keep only the top 12 items.

      The difference is that you want to differentiate only by air_date , while the normal use of orderby is the use of both parameters Does it to determine that the item ends in the result.

      I do not know an easy way to solve it, though. However, since you only want to change the display order of the resulting items, you can only fix it yourself, instead of get_posts WP_Query And PHP's

        $ posts = get_posts (...); Usort ($ posts, '__sort_by_date_desc'); Create a timestamp from the function __sort_by_date_desc ($ a, $ b) {// MySQL date time values ​​$ a_date = mysql2date ('U', $ a-> post_date); $ B_date = mysql2date ('u', $ b- & gt; post_date); // In descending order, swap these to return withdrawals $ b_date - $ a_date; }    

  • No comments:

    Post a Comment