admin管理员组文章数量:1130349
I have a custom WP_Query with 2 different meta queries. I want to order the results of both queries differently. The first meta_query I want to order by a custom field (high integer - low integer). The second meta_query I want to order by date. To be clear, I want to first show the results from the first query and then the results from the second query.
Below the code I currently have. For some reason it is ordering both meta_query by meta_value_num. How can I order both meta queries differently, within the same WP_Query? Or do I actually have to make 2 different WP queries?
<?php
$sticky = get_option( 'sticky_posts' );
$term = get_queried_object();
/* Query sticky posts */
$the_query = new WP_Query( array( 'post__in' => $sticky, 'ignore_sticky_posts' => 1, 'posts_per_page' => 5, 'post_type' => 'ads', 'tax_query' => array(
//Get posts by term slug
'relation' => 'OR',
array (
'taxonomy' => 'ad_cat',
'field' => 'slug',
'terms' => $term->slug,
)
),
'meta_query' => array(
//Get posts with custom field key 'cc_aff_stick_top'
'relation' => 'OR',
'is_aff' => array(
'key' => 'cc_aff_stick_top',
'compare' => 'EXISTS',
),
//Get posts without key
'is_not_aff' => array(
'key' => 'cc_aff_stick_top',
'compare' => 'NOT EXISTS',
),
),
//Order by CF cc_aff_stick_top and otherwise by date
'orderby' => array(
'meta_value_num' => 'ASC',
'date' => 'DESC'
),
) );
// The Loop
if ( $the_query->have_posts() ) {
I have a custom WP_Query with 2 different meta queries. I want to order the results of both queries differently. The first meta_query I want to order by a custom field (high integer - low integer). The second meta_query I want to order by date. To be clear, I want to first show the results from the first query and then the results from the second query.
Below the code I currently have. For some reason it is ordering both meta_query by meta_value_num. How can I order both meta queries differently, within the same WP_Query? Or do I actually have to make 2 different WP queries?
<?php
$sticky = get_option( 'sticky_posts' );
$term = get_queried_object();
/* Query sticky posts */
$the_query = new WP_Query( array( 'post__in' => $sticky, 'ignore_sticky_posts' => 1, 'posts_per_page' => 5, 'post_type' => 'ads', 'tax_query' => array(
//Get posts by term slug
'relation' => 'OR',
array (
'taxonomy' => 'ad_cat',
'field' => 'slug',
'terms' => $term->slug,
)
),
'meta_query' => array(
//Get posts with custom field key 'cc_aff_stick_top'
'relation' => 'OR',
'is_aff' => array(
'key' => 'cc_aff_stick_top',
'compare' => 'EXISTS',
),
//Get posts without key
'is_not_aff' => array(
'key' => 'cc_aff_stick_top',
'compare' => 'NOT EXISTS',
),
),
//Order by CF cc_aff_stick_top and otherwise by date
'orderby' => array(
'meta_value_num' => 'ASC',
'date' => 'DESC'
),
) );
// The Loop
if ( $the_query->have_posts() ) {
本文标签: wp queryOrder 2 metaqueries differently in WPQuery
版权声明:本文标题:wp query - Order 2 meta_queries differently in WP_Query? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749028482a2305482.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论