admin管理员组文章数量:1130349
I'm having trouble to understand customizing the new WP_query. I've found the script below, partly, here in order to get the sticky posts. I've further edited it in order to:
- first get the (sticky) posts with the custom field 'cc_aff_stick_top' and order them DESC (high integer - low integer)
- Then DESC sort the sticky other posts (without that custom field) based on date.
Especially showing/sorting the second point, I find confusing. I tried it with
'is_not_aff' => array(
'key' => 'cc_aff_stick_top',
'compare' => 'NOT EXISTS',
),
Question
- That seems to be working, but I am pretty sure this is not the right way to go. Who can help me out?
- Also, I am wondering why
meta_queryneeds to be there. I only want to query posts based on thetax_query. Is it correct thatmeta_queryactually does another query of the posts? Ismeta_queryrequired in order to sort on multiple values? - How can I sort the posts without the CF
cc_aff_stick_topby date? Adding'date' => 'DESC'in the'orderby' => arrayis not working.
Answer
Works: 'orderby' => array( 'meta_value_num' => 'DESC', 'date' => 'ASC' )
Works: Setting 'type' => 'NUMERIC' in the meta_query, and 'orderby' => array( 'is_aff' => 'DESC', 'date' => 'DESC' )
The code:
/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );
//Get term
$term = get_queried_object();
if (!empty($sticky)) {
/* Sort the stickies with the newest ones at the top */
rsort( $sticky );
/* Get the 5 newest stickies (change 5 for a different number) */
$sticky = array_slice( $sticky, 0, 50 );
/* Query sticky posts */
$the_query = new WP_Query( array( 'post__in' => $sticky, 'ignore_sticky_posts' => 1, 'post_type' => 'ad_listing', 'tax_query' => array(
'relation' => 'OR',
array (
'taxonomy' => 'ad_cat',
'field' => 'slug',
'terms' => $term->slug,
),
//Posts with the slug below should be shown everywhere
array (
'taxonomy' => 'ad_cat',
'field' => 'slug',
'terms' => 'cc-slug',
)
),
'meta_query' => array(
'relation' => 'OR',
'is_aff' => array(
'key' => 'cc_aff_stick_top',
'compare' => 'EXISTS',
),
'is_not_aff' => array(
'key' => 'cc_aff_stick_top',
'compare' => 'NOT EXISTS',
),
),
'orderby' => array(
'is_aff' => 'ASC',
'is_not_aff' => 'DESC',
),
) );
// The Loop
I'm having trouble to understand customizing the new WP_query. I've found the script below, partly, here in order to get the sticky posts. I've further edited it in order to:
- first get the (sticky) posts with the custom field 'cc_aff_stick_top' and order them DESC (high integer - low integer)
- Then DESC sort the sticky other posts (without that custom field) based on date.
Especially showing/sorting the second point, I find confusing. I tried it with
'is_not_aff' => array(
'key' => 'cc_aff_stick_top',
'compare' => 'NOT EXISTS',
),
Question
- That seems to be working, but I am pretty sure this is not the right way to go. Who can help me out?
- Also, I am wondering why
meta_queryneeds to be there. I only want to query posts based on thetax_query. Is it correct thatmeta_queryactually does another query of the posts? Ismeta_queryrequired in order to sort on multiple values? - How can I sort the posts without the CF
cc_aff_stick_topby date? Adding'date' => 'DESC'in the'orderby' => arrayis not working.
Answer
Works: 'orderby' => array( 'meta_value_num' => 'DESC', 'date' => 'ASC' )
Works: Setting 'type' => 'NUMERIC' in the meta_query, and 'orderby' => array( 'is_aff' => 'DESC', 'date' => 'DESC' )
The code:
/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );
//Get term
$term = get_queried_object();
if (!empty($sticky)) {
/* Sort the stickies with the newest ones at the top */
rsort( $sticky );
/* Get the 5 newest stickies (change 5 for a different number) */
$sticky = array_slice( $sticky, 0, 50 );
/* Query sticky posts */
$the_query = new WP_Query( array( 'post__in' => $sticky, 'ignore_sticky_posts' => 1, 'post_type' => 'ad_listing', 'tax_query' => array(
'relation' => 'OR',
array (
'taxonomy' => 'ad_cat',
'field' => 'slug',
'terms' => $term->slug,
),
//Posts with the slug below should be shown everywhere
array (
'taxonomy' => 'ad_cat',
'field' => 'slug',
'terms' => 'cc-slug',
)
),
'meta_query' => array(
'relation' => 'OR',
'is_aff' => array(
'key' => 'cc_aff_stick_top',
'compare' => 'EXISTS',
),
'is_not_aff' => array(
'key' => 'cc_aff_stick_top',
'compare' => 'NOT EXISTS',
),
),
'orderby' => array(
'is_aff' => 'ASC',
'is_not_aff' => 'DESC',
),
) );
// The Loop
本文标签: wp querySort posts on custom field AND after that sort on date
版权声明:本文标题:wp query - Sort posts on custom field AND after that sort on date? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749198059a2331323.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论