0

I use following array and works fine... sort post by meta_key where value of t_count is a number:

$args = array(
 'posts_per_page' => 10,
 'meta_key' => 't_count',
  'orderby' => 'meta_value',
   );
      query_posts($args);

I use 3.4.2 but if I upgrade to 3.5 array above do not sort correctly.

I've try also meta_value_num but do not works fine.

Could you help me?

Thanks

1 Answer 1

0

Try WP_Query() instead of query_posts()

$args = array(
   'posts_per_page' => 10,
   'meta_key' => 't_count',
   'orderby' => 'meta_value_num',
   'order' => 'ASC'
 );

// The Query
$the_query = new WP_Query( $args );

// The Loop
while ( $the_query->have_posts() ) :
    $the_query->the_post();
    echo '<li>' . get_the_title() . '</li>';
endwhile;

// Restore original Query & Post Data
wp_reset_query();
wp_reset_postdata();

Above code should work!

4
  • Thanks @Furqan but do not works. Example meta_value array stored a:2:{s:5:"count";d:31714;s:7:"timeout";i:1358206247;}
    – Marco
    Jan 14, 2013 at 22:56
  • are you using "WPAlchemy MetaBox PHP Class" ? Jan 15, 2013 at 13:50
  • No, I use: update_post_meta( $post_id, 't_count', $meta );
    – Marco
    Jan 15, 2013 at 17:51
  • yes! 'orderby' => 'meta_value_num' is only work with single value not with serialized array. i think you have to hack this but i have no idea about this. another solution is have a look at this video youtube.com/watch?v=5i6nOSVOBaY use wpalchemy-metabox (farinspace.com/wpalchemy-metabox) Jan 15, 2013 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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