0

Please take a look at the loops below, I'm having issues trying to order the attachment by title, name, and modified.

<?php
                        foreach ($instance['data'] as $id) :
                                $args = array(
                                    'include' => $id,
                                    'post_type' => 'attachment',                                    
                                    'posts_per_page' => -1,
                                    'post_status' => 'any',
                                    'orderby' => 'title',
                                    'order' => 'DESC',                                  
                                );
                                $data_attach = get_posts($args);
                                foreach ($data_attach as $data) :                                   
                                    ?>          
                                    <?php print_r($data->ID);?>                                     
                                    <?php
                                endforeach;                                
                        endforeach;
                        ?>

Print_R's

print_r($instance['data']);

Array ( [0] => 1686 [1] => 1628 [2] => 1027 [3] => 1022 [4] => 1045 [5] => 1029 ) 

print_r($data->ID);

1686 1628 1027 1022 1045 1029 
  • Did you try 'orderby' => 'menu_order' to see if that changes the order at all because I have doubts that WP will do the sorting for attachments. – Wali Hassan May 15 '15 at 16:11
  • I've just tried it. Getting the same results. This is the print_r 1686 1628 1027 1022 1045 1029 – KC Chai May 15 '15 at 16:13
  • Yes, so I think you have to manually sort the results. Wordpress is not going to do it for you there. – Wali Hassan May 15 '15 at 16:15
  • However, if I do not use the include in $ args it worked but this is not I wanted. – KC Chai May 15 '15 at 16:29
1

You don't get any ordering, because you are making multiple get_posts() calls and only retrieve a single post in each of them. You cannot order a single post. :)

You need to get rid of outermost loop and just pass a set of IDs (that would be your $instance['data']) to retrieve via single get_posts() call.

  • Hi, thanks for your answer. I still couldnt figure out how to pass a set of IDs without using a loop to retrive it. Please show a me code. $instance ['data'] is an Array. – KC Chai May 15 '15 at 18:46
  • 'include' => $instance ['data'] – Rarst May 15 '15 at 19:10
0

Like I said, in your custom scenario, after getting the results, here is your how you can do it:

$data_attach = get_posts($args);
        $title_array = array();

        foreach ($data_attach as $data) :                                   
            $title_array[] = $data->post_title;
        endforeach;

        sort($title_array);
            foreach ($title_array as $key => $val) {
                echo $val . "<br />";
            }  

Also, refer to this : http://php.net/manual/en/array.sorting.php

  • Hi Wali, those numbers are actually attachment ids. Im trying to sort by attachment titles, names, dates or modified. – KC Chai May 15 '15 at 16:34
  • I just edited my Answer to give you a better idea of how to sort the results. – Wali Hassan May 15 '15 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.