gurung
gurung

Reputation: 626

sort post attachments by title order (numerical) - wordpress

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.

Upvotes: 0

Views: 1390

Answers (1)

cpilko
cpilko

Reputation: 11852

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' );

Upvotes: 3

Related Questions