Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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' );
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.