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; } |










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
@ 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:2
3
get_posts ( $args ) ; $ Posts = get_posts ($ args);
...
[...] 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 [...]
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
@ 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:
Now I do not know the names of the fields, but it is enough that you enter at the end:
o non so. Where is
camponame of the table field, typenomeor do not know.Hard to be exhaustive Here ...