admin管理员组文章数量:1130349
I've got a fairly complicated post type archive page that I need to order by two meta fields and the post title. To do that, I'm using pre_get_posts with a meta_query that simply tests for the existence of two meta-keys. It's entirely to allow for the complex ordering required. This wasn't working until I accidentally used the wrong syntax.
Why does this code work? Update: "Fixed" snippet to show the wrong-but-functioning code. h/t @SallyCJ. Update 2: Fixed the fixed snippet so keys are correct.
<?php
add_action( 'pre_get_posts', 'wpse_pre_get_posts' );
function wpse_pre_get_posts( $query ) {
if( ! $query->is_main_query() ) {
return;
}
if( is_post_type_archive( 'my-post-type' ) ) {
$query->set( 'meta_query', array(
'relation' => 'OR',
array( // this array shouldn't be here but is required for the ordering to work
'meta_query_1' => array(
'key' => 'cis_schools_is_model',
'type' => 'numeric',
'compare' => 'EXISTS',
),
'meta_query_2' => array(
'key' => 'cis_schools_district',
'compare' => 'EXISTS',
),
),
));
$query->set( 'orderby', array(
'meta_query_1' => 'DESC',
'meta_query_2' => 'ASC',
'title' => 'ASC',
));
}
}
My expectation is that the extra array wrapping the two keyed meta queries (meta_query_1 and meta_query_2) shouldn't be necessary like this:
<?php
$query->set( 'meta_query', array(
'relation' => 'OR',
'meta_query_1' => array(
'key' => 'meta-key-1',
'type' => 'numeric',
'compare' => 'EXISTS',
),
'meta_query_2' => array(
'key' => 'meta-key-two',
'compare' => 'EXISTS',
),
));
However, the first snippet works and the second doesn't.
I've got a fairly complicated post type archive page that I need to order by two meta fields and the post title. To do that, I'm using pre_get_posts with a meta_query that simply tests for the existence of two meta-keys. It's entirely to allow for the complex ordering required. This wasn't working until I accidentally used the wrong syntax.
Why does this code work? Update: "Fixed" snippet to show the wrong-but-functioning code. h/t @SallyCJ. Update 2: Fixed the fixed snippet so keys are correct.
<?php
add_action( 'pre_get_posts', 'wpse_pre_get_posts' );
function wpse_pre_get_posts( $query ) {
if( ! $query->is_main_query() ) {
return;
}
if( is_post_type_archive( 'my-post-type' ) ) {
$query->set( 'meta_query', array(
'relation' => 'OR',
array( // this array shouldn't be here but is required for the ordering to work
'meta_query_1' => array(
'key' => 'cis_schools_is_model',
'type' => 'numeric',
'compare' => 'EXISTS',
),
'meta_query_2' => array(
'key' => 'cis_schools_district',
'compare' => 'EXISTS',
),
),
));
$query->set( 'orderby', array(
'meta_query_1' => 'DESC',
'meta_query_2' => 'ASC',
'title' => 'ASC',
));
}
}
My expectation is that the extra array wrapping the two keyed meta queries (meta_query_1 and meta_query_2) shouldn't be necessary like this:
<?php
$query->set( 'meta_query', array(
'relation' => 'OR',
'meta_query_1' => array(
'key' => 'meta-key-1',
'type' => 'numeric',
'compare' => 'EXISTS',
),
'meta_query_2' => array(
'key' => 'meta-key-two',
'compare' => 'EXISTS',
),
));
However, the first snippet works and the second doesn't.
本文标签: meta queryWhy does this incorrect pregetposts metaquery work for sort order
版权声明:本文标题:meta query - Why does this incorrect pre_get_posts meta_query work for sort order? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749233962a2336965.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论