0

I am working on a site, I want the results to display alphabetically. A prevous developer has setup this code.

if(!empty($post_data)){ 
                echo '<div class="related-attorneys">
                    <h3>related attorneys</h3>
                    <ul>';                      

                    for ($i=0; $i <=end($post_keys) ; $i++) { 


                         echo '<li><a href="' .get_permalink( $post_data[$i]->ID ).'">' .$post_data[$i]->post_title. '</a></li>';
                    }
                echo '</ul></div>'; 
                }

I want to order the results based off the titles. I have tried a lot of examples but nothing seems to work. the array $post_data looks like this:

Array ( [0] => WP_Post Object ( [ID] => 4870 [post_author] => 2 [post_date] => 2017-08-08 20:39:20 [post_date_gmt] => 2017-08-08 20:39:20  [post_name] => michael-d-leedberg [post_title] => Mark J. Hoover

Any help would be appreciated.

  • so the problem is with the output, or with the ordering of the posts? – spielerds Jun 13 '18 at 17:43
  • Yes, the post_titles show completely randomly I want it to just output alphabetically. – Edward Novak Jun 13 '18 at 17:46
  • I think the results should be ordered there, where you (or wordpress) fetch it from the db. But if you insist on doing it here, take a look at this: https://stackoverflow.com/questions/4282413/sort-array-of-objects-by-object-fields – spielerds Jun 13 '18 at 17:52
  • 1
    try putting this after the if: `usort($post_data, function ($a, $b) { return strcmp($a->post_title, $b->post_title); });` – spielerds Jun 13 '18 at 17:58

0 Answers0