0

I had a post type music. For which i created custom field Music length. Which store music duration in this format like 01:50 I want to display post on order by Music length. I had use both order by meta_value and meta_value_num. But it does not work. My code to get post is:

<code>
$args = array(
  'post_type' => 'music',
  'meta_key' => 'length',
  'orderby' => 'meta_value_num',
  'order' => 'DESC',
  'posts_per_page' => 10
);
$pop_posts = new WP_Query( $args );
</code>

My question is how to order the post by its meta value in semicolon format(01:50, 02:46, 03:04, 02:37). So the post with meta value 03:04 comes first then 02:46 and so on! Is there any way?

4

1 回答 1

1

使用以下代码

$args = array(
  'post_type' => 'music',
  'meta_key' => 'length',
  'orderby' => "REPLACE(meta_value_num, ':', '')",
  'order' => 'DESC',
  'posts_per_page' => 10
);
$pop_posts = new WP_Query( $args );

但请注意,对于大型数据集,它会非常慢,因为它必须为每一行重新计算新字符串。

于 2017-06-12T07:42:35.817 回答