How to set sorting the list alphabetically?

Hi! I'm a little stuck... I have code that gets the user's bookmark list (titles):

/**
 * Print the list of bookmarks
*
 * @param string $post_type
 * @param int $user_id
 * @param int $limit
 * @param bool $show_remove
*/
 function display_favlist( $post_type = 'all', $user_id = false, $limit = 10, $show_remove = true ) {

 $posts = $this->get_favorites( $post_type, $user_id, $limit );

 echo '<div class="favclass"><ul>';
 if ( $posts ) {

 $remove_title = __( 'Remove from list 'wl1' );
 $remove_link = ' <a href="#" data-id="%s" title="%s" class="wl1-remove-favorite">x</a>';

 foreach ($posts as $item) {
 $extra = $show_remove ? sprintf( $remove_link, $item->post_id, $remove_title ) : ";
 printf( '<li><a href="%s">%s</a>%s</li>', get_permalink( $item->post_id ), get_the_title( $item->post_id ), $extra );
}
 } else {
 printf( '<li>%s</li>', __( 'No bookmarks', 'wl1' ) );
}
 echo '</ul></div>';
 }


How you can set here the list sorted alphabetically?
June 5th 19 at 21:48
2 answers
June 5th 19 at 21:50
Use the sort function. usort, for example.
June 5th 19 at 21:52
It is better not to sort the output in a loop, and upon receipt of all entries. In your case is responsible for it here's the code:

$posts = $this->get_favorites( $post_type, $user_id, $limit );


Show that there is in the method get_favorites(). Judging by the fact that you are working with array of objects, inside this method it calls get_posts(). Ask him to sort all your posts at the stage of sampling from the database.
Why to change the behavior of a method? In this case, where appropriate to make the trivial sorting of the array. - Lonie.Batz commented on June 5th 19 at 21:55
well, firstly because the method obviously stupid proxy on get_posts, forming it from the received parameters (the type of post the user how many records). The method is not final and I see no problem why suddenly it is impossible to change his behavior. You can sehardcore sorting, you can pass additional parameter. What is the problem to change the method and its signature, if necessary? Religion?

Second, it is possible the limit may be used for paging/batch output, or even for simple record limits the number of X, but in this way the sorting will be applied to each portion of the records independently. That is, you will not get "all my favorites alphabetically", but my favorites by date, but sorted alphabetically. I hope the difference in the resulting list visible. As really the default should be - is another question, but the fact that here there is a potential non-obvious bug is obvious.

Thirdly, ideologically correct to do the sorting as close as possible to the data source, especially if he is well able to do it. Why the extra overhead in php?

In the fourth, seen another very obvious point - that of default on the output there is a button "remove from favourites". After removal is not entirely clear what's going on - pulled a new list entirely? The length of the list decreases? Pulled 1 extra item? Or maybe there's a buffer of several elements, which will allow you just to fill out the list? By itself, this obvious point is not critical now, but knowing it can be additional understanding, is it possible to do a simple sort on output, or is it better to do the sample correctly in the first place?

IMHO, if it is possible to choose from the database initially, and not to build crutches. - Velva.Rogahn commented on June 5th 19 at 21:58
that is the right approach. But whether in this case the TS have to do right? Probably not.

In any case, thanks for the detailed answer. - Lonie.Batz commented on June 5th 19 at 22:01
But whether in this case the TS have to do right?

I can certainly nerd (actually Yes), but IMHO it is necessary to do always, even if the specific task / situation / the project does not require this right. It is a question of personal hygiene and quality growth of their own skills. - Velva.Rogahn commented on June 5th 19 at 22:04
Do you offer this method usort paste?
function get_favorites( $post_type = 'all', $user_id = 0, $count = 10, $offset = 0 ) {
 $where = 'WHERE user_id = ';
 $where .= $user_id ? $user_id : get_current_user_id();
 $where .= $post_type == 'all' ? ": "AND post_type = '$post_type'";


 $sql = "SELECT post_id, post_type
 FROM {$this->table}
$where
 GROUP BY post_id
 ORDER BY post_type
 LIMIT $offset, $count";

 $result = $this->db->get_results( $sql );

 return $result;
 }
- weston_Ort commented on June 5th 19 at 22:07
, well, as I expected, there is still offset, and thus at least some semblance of pagination/portionate. So, usort will give exactly the problems that I described in the comments above.

do you have a custom DB query. Copy it to sorted alphabetically and not by post_type. - Velva.Rogahn commented on June 5th 19 at 22:10

Find more questions by tags WordPress