title: sort post attachments by title order (numerical) - wordpress
body: <p>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.</p>
  
  <pre><code>$images =&amp; get_children( array (
              'post_parent' =&gt; $post-&gt;ID,
              'post_type' =&gt; 'attachment',
              'post_mime_type' =&gt; 'image',
              'orderby' =&gt; 'title',
              'order' =&gt; 'ASC'
              ));
  </code></pre>
  
  <p>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 <a href="https://stackoverflow.com/questions/9714928/wordpress-title-numeric-sort-function?rq=1">here</a> and <a href="https://stackoverflow.com/questions/15977188/wordpress-posts-orderby-filter-with-custom-table-in-plugin">there</a> I have put put together a code below but does not seem to work. Could you please help me correct this.</p>
  
  <pre><code>function orderby_post_title_int( $orderby ) {
              global $wpdb;
              if(!is_admin &amp;&amp; is_single()){
              $orderby = '(wp_posts.post_title+0) ASC';
              return $orderby;
              }
              }
              add_filter('posts_orderby', 'orderby_post_title_int' );
  
                    $images =&amp; get_children( array (
                'post_parent' =&gt; $post-&gt;ID,
                'post_type' =&gt; 'attachment',
                'post_mime_type' =&gt; 'image',
                'orderby' =&gt; 'title',
                'order' =&gt; 'ASC'
                    ));
              } 
              remove_filter('posts_orderby', 'orderby_post_title_int' );
  </code></pre>
  
  <hr>
  
  <p>Here is a result of <code>&lt;pre&gt;print_r($images)&lt;/pre&gt;</code> looks like (only the first post)</p>
  
  <pre><code>Array
  (
      [306] =&gt; WP_Post Object
          (
              [ID] =&gt; 306
              [post_author] =&gt; 1
              [post_date] =&gt; 2014-06-09 17:01:29
              [post_date_gmt] =&gt; 2014-06-09 17:01:29
              [post_content] =&gt; 
              [post_title] =&gt; 120
              [post_excerpt] =&gt; 
              [post_status] =&gt; inherit
              [comment_status] =&gt; open
              [ping_status] =&gt; open
              [post_password] =&gt; 
              [post_name] =&gt; some_file_name
              [to_ping] =&gt; 
              [pinged] =&gt; 
              [post_modified] =&gt; 2014-06-09 17:01:29
              [post_modified_gmt] =&gt; 2014-06-09 17:01:29
              [post_content_filtered] =&gt; 
              [post_parent] =&gt; 305
              [guid] =&gt; http://xxxxxxx/wp-content/uploads/2014/06/xxxxxxxxxx.jpg
              [menu_order] =&gt; 0
              [post_type] =&gt; attachment
              [post_mime_type] =&gt; image/jpeg
              [comment_count] =&gt; 0
              [filter] =&gt; raw
          )
  </code></pre>
  
  <p>Upvotes of the <a href="https://stackoverflow.com/questions/8765438/wordpress-post-page-title-numeric-sort">similar question here</a> proves that people have been looking for an answer for this, sadly the answer in the thread is solves nothing.</p>