WordPress: Order a series of posts for any field

o un array di oggetti. The technique that I describe here is useful in many situations where, for example, we have an array of array or an array of objects. , senza aver accesso alla select originale (sul database) che ha generato l'elenco (array) stesso: In WordPress can sometimes need to manipulate a list of posts, in the standard array of stdClass Object without having access to the original select (on the database) that generated the list (array) itself:

1
2
3
4
5
6
7
Array
(
[0] => stdClass Object
(
[ID] => 104
[Post_author] => 37
...

. However, you can order such an array at a later time, without going through the database, using the php function usort() . This allows you to perform a sort using a function callback personnel. o un altro stdClass Object : For example, imagine that you want to order our post post_author or for any other field that, in turn, can be an array or another stdClass Object :

1
2
3
4
5
6
7
8
9
10
/ **
* $ Posts array of objects is our "post" stdClass Object
* /
$posts , "__sort" ) ; usort ($ posts, "__sort");

$a , $b ) { function __ sort ($ a, $ b) {
$a -> post_author == $b -> post_author ) return 0 ; if ($ a -> post_author == $ b -> post_author) return 0;
$a -> post_author > $b -> post_author ) ? - 1 : 1 ; // decrescente return ($ a -> post_author> $ b -> post_author)? - 1: 1, / / descending
/ / Return ($ a-> post_author <$ b-> post_author)? -1: 1, / / increasing
}

Playing on more ">" and less "<" is the last statement can be ordered in ascending or descending order.

These situations can be very frequent in advanced contexts where, for example, will have a series of lists to be scanned. Another example is in the case of combinations, where several lists of posts, the limit recovered from different blogs, must be ordered.

1
2
3
4
5
6
7
8
9
10
11
$posts , $lista_1 , $lista_2 , $lista_3 , ..., $lista_n ) ; array_push ($ posts, $ lista_1, $ lista_2, lista_3 $, ..., $ lista_n);
/ **
* $ Posts is an array of lists, lists of retrieved post who knows
* Where and who knows how ...
* /
$posts , "__sort" ) ; usort ($ posts, "__sort");

$a , $b ) { function __ sort ($ a, $ b) {
$a -> ID == $b -> ID ) return 0 ; if ($ a -> ID == $ b -> ID) return 0;
$a -> ID < $b -> ID ) ? - 1 : 1 ; return ($ a -> ID <$ b -> ID)? - 1: 1;
}

5 comments to: ""

  1. March 28, 2011 elia :

    hello,
    I read your article and is close to what I should do.
    I have to sort the posts based on the value of different custom field;
    for example sort by:
    - Price
    - Name
    - Place
    what is the best way to proceed?
    thanks

  2. November 3, 2011 Giovan Battista Fazioli :

    @ Elia :: the best way is to use the meta_query , introduced with version 3.1 of WordPress. Find examples and documentation here . For example, you can retrieve all of the posts that have a meta_key price equal to 100:

    1
    2
    3
    array ( 'meta_key' => 'prezzo' , 'meta_value' => '100' ) ; $ Args = array ('meta_key' => 'price', 'meta_value' => '100 ');
    get_posts ( $args ) ; $ Posts = get_posts ($ args);
    ...
  3. November 3, 2011 Order an array of arrays in PHP - Undolog.com - Undolog.com :

    [...] PHP function usort () view to WordPress: Order a series of posts for any field can be easily used also to order an array of arrays. For example, if we [...]

  4. June 6, 2012 Lorenzo_l :

    I'm sorry if my question is incomprehensible to you, I'm sorry but I do not know the exact tarminologia to make me understand better.

    I run a blog created by a friend, who now nor longer able to follow him.
    We've created a table that acts as an index to the various links. I create pages "deck" and links to these are automatically inserted in the table. At the moment, I do not have the ability to sort these links in an arbitrary way!
    They are displayed in descending order of insertion.

    There is the possibility of implementing a code so that it may, from time to time, decide where to put the links to the pages I edited inside the Matrix?
    And example: "alphabetically" or "type" or "importance"?

    thank you very much for your availability!
    Lorenzo

  5. June 7, 2012 Giovan Battista Fazioli :

    @ Lorenzo_l: In part, I think I know, and of course it is possible. First you track down where the program would be enough SELECT SQL that extracts the data there and add your own sort. You'd need, ultimately, to a technical intervention that should be resolved in a few minutes.
    If you want to try, of course with the few indications I have, you can try to add sorting by myself.
    The SELECT statement should look like:

    1
    FROM .... SELECT * FROM ....

    Now I do not know the names of the fields, but it is enough that you enter at the end:

    1
    FROM ... ORDER BY campo SELECT * FROM ... ORDER BY field

    o non so. Where is campo name of the table field, type nome or do not know.
    Hard to be exhaustive Here ... :)

Leave a comment

TAG XHTML PERMITS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> CODE ENTRY:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL