admin管理员组文章数量:1122846
I'm working with lumber and have custom post types with ACF fields for height and width in decimals.
I'm trying to sort my posts first by height and then by width. I am using the following code:
// sort stock and custom profiles by height first then width
add_action( 'pre_get_posts', function ( $query ) {
if( is_admin() ) {
return $query;
}
// Check if post_type is either 'stock' or 'custom'
if( isset($query->query_vars['post_type']) && ($query->query_vars['post_type'] == 'stock' || $query->query_vars['post_type'] == 'custom') ) {
$meta_query = array(
'relation' => 'AND',
'clause1' => array(
'key' => 'height_decimal',
'compare' => 'EXISTS',
),
'clause2' => array(
'key' => 'width_decimal',
'compare' => 'EXISTS',
)
);
$query->set('meta_query', $meta_query);
$query->set('orderby', array(
'clause1' => 'ASC',
'clause2' => 'ASC'
));
}
// Return the modified query
return $query;
} );
This is working great in post archive and taxonomy archive pages. But I have a filter on the categories and when the filter is used they stay in order BUT it switches to a lexicographic order instead of numeric.
So on the taxonomy archive page the order is:
0.1 0.200 0.3 0.4
But when the Ajax filter is applied the order switches to:
0.1 0.3 0.4 0.200
I have tried adding
'type' => 'NUMERIC',
to my two clauses but that removes the custom sorting all together.
本文标签: custom post typesMeta query sort order is lexigraphical instead of numeric
版权声明:本文标题:custom post types - Meta query sort order is lexigraphical instead of numeric 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736300087a1930692.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论