0

I have been assigning numerical values to all the post attachments as their titles. I am trying to set order of these attachments based on their titles, which is a numerical value then I was trying to use a simple argument like the one shown below.

$images =& get_children( array (
            'post_parent' => $post->ID,
            'post_type' => 'attachment',
            'post_mime_type' => 'image',
            'orderby' => 'title',
            'order' => 'ASC'
            ));

But this just gives something in this order 10, 1, 21, 29, 2 ..... which is more of an alphabetical sorting rather than a numerical sorting I seek eg 1, 2, 10, 21, 29 ... I am aware of the meta_value_num function but I am not dealing with custom fields here. Also FYI this is NOT the only loop on page, as I have two other loops on my single.php. Inspired from here and there I have put put together a code below but does not seem to work. Could you please help me correct this.

function orderby_post_title_int( $orderby ) {
            global $wpdb;
            if(!is_admin && is_single()){
            $orderby = '(wp_posts.post_title+0) ASC';
            return $orderby;
            }
            }
            add_filter('posts_orderby', 'orderby_post_title_int' );

                  $images =& get_children( array (
              'post_parent' => $post->ID,
              'post_type' => 'attachment',
              'post_mime_type' => 'image',
              'orderby' => 'title',
              'order' => 'ASC'
                  ));
            } 
            remove_filter('posts_orderby', 'orderby_post_title_int' );

Here is a result of <pre>print_r($images)</pre> looks like (only the first post)

Array
(
    [306] => WP_Post Object
        (
            [ID] => 306
            [post_author] => 1
            [post_date] => 2014-06-09 17:01:29
            [post_date_gmt] => 2014-06-09 17:01:29
            [post_content] => 
            [post_title] => 120
            [post_excerpt] => 
            [post_status] => inherit
            [comment_status] => open
            [ping_status] => open
            [post_password] => 
            [post_name] => some_file_name
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2014-06-09 17:01:29
            [post_modified_gmt] => 2014-06-09 17:01:29
            [post_content_filtered] => 
            [post_parent] => 305
            [guid] => http://xxxxxxx/wp-content/uploads/2014/06/xxxxxxxxxx.jpg
            [menu_order] => 0
            [post_type] => attachment
            [post_mime_type] => image/jpeg
            [comment_count] => 0
            [filter] => raw
        )

Upvotes of the similar question here proves that people have been looking for an answer for this, sadly the answer in the thread is solves nothing.

Community
  • 1
  • 1
gurung
  • 628
  • 2
  • 11
  • 33

1 Answers1

3

The order you are trying to get is called a "natural sort". Wordpress doesn't support it, so you'll need to sort your result after the query.

This code works with get_posts', which should be similar to the result ofget_children`. Try this immediately after your first code snippet above:

function natural_sort($a, $b) {
   return strnatcmp($a->post_title, $b->post_title);
};
usort($images->posts, 'natural_sort' );
Community
  • 1
  • 1
cpilko
  • 11,792
  • 2
  • 31
  • 45
  • it throws this - `Warning: Attempt to modify property of non-object in.....line 60` and also `Warning: usort() expects parameter 1 to be array, null given in.... line 68` wherein the line 68 is `usort($images->posts, 'natural_sort' );` – gurung Jun 10 '14 at 20:30
  • I can assure you that $images is a legal array with a lot of posts information, which I got when I did `echo '
    '; print_r($images); echo '
    ';` This funtion `strnatcmp` does not seem to agree with wordpress environment or some override I really have no clue. I tried this before from your [previous answer here](http://stackoverflow.com/questions/23520919/natural-sort-wordpress-post-titles-alphabetically-and-numerically/23523621#23523621)
    – gurung Jun 10 '14 at 20:37
  • The error on line 60 may or may not be related. Take a look at your `print_r` and make sure there is a `$images->posts->title` for each result. – cpilko Jun 10 '14 at 20:40
  • please check the bottom of question for print result, you see the post_title is intact. Also my code-editor still throws syntax-error for `your code on line 3` says - unexpected `')'` expecting `$EOF` – gurung Jun 10 '14 at 20:48
  • FYI I have php version 5.4.29 running on my server, if it matters. – gurung Jun 10 '14 at 20:57
  • the code with `)` removed still gives me the warning i stated in the first comment – gurung Jun 10 '14 at 20:59