admin管理员组文章数量:1319000
I'm trying to sort the custom posts based on meta value as priority level1, level2 and level3. I have given a dropdown field in post meta as featured posts, standard posts and basic posts. I want to display them as featured posts on top, then standard posts and then basic posts. I tried with below code but it's not sorting the posts in the order.
Any guidance or advise much appreciated!
Here is my code:
Array(
'name' => __( 'Listing Type Options', 'text-domain' ),
'id' => 'lising_type_options',
'type' => 'select',
'child_of' => '',
'options' => array(
'' => 'Select Listing Type',
'1' => 'Basic',
'2' => 'Standard',
'3' => 'Featured',
),
'desc' => ''
),
if( isset( $lp_type ) && !empty( $lp_type ) ){
if( $lp_type == '3' ){
$lp_type = 'Featured';
} elseif( $lp_type == '2' ){
$lp_type = 'Standard';
} elseif( $lp_type == '1' ){
$lp_type = 'Basic';
}
} else{
$lp_type = '';
}
This is the query:
$args = array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => $postsonpage,
'paged' => $paged,
'post__not_in' =>$ad_campaignsIDS,
'tax_query' => $TxQuery,
// 'meta_key' => $MtKey,
// 'orderby' => $lporderby,
// 'order' => $lporders,
'order' => 'DESC',
'orderby' => 'meta_value',
'meta_query' => array(
'relation' => 'OR',
$MtKey,
array(
'relation' => 'OR',
array(
'key' => 'lp_listingpro_options',
'value' => 'lising_type_options',
'compare' => 'LIKE',
),
array(
'key' => 'lp_listingpro_options',
'value' => 'lising_type_options',
'compare' => 'NOT LIKE',
),
),
),
);
I'm trying to sort the custom posts based on meta value as priority level1, level2 and level3. I have given a dropdown field in post meta as featured posts, standard posts and basic posts. I want to display them as featured posts on top, then standard posts and then basic posts. I tried with below code but it's not sorting the posts in the order.
Any guidance or advise much appreciated!
Here is my code:
Array(
'name' => __( 'Listing Type Options', 'text-domain' ),
'id' => 'lising_type_options',
'type' => 'select',
'child_of' => '',
'options' => array(
'' => 'Select Listing Type',
'1' => 'Basic',
'2' => 'Standard',
'3' => 'Featured',
),
'desc' => ''
),
if( isset( $lp_type ) && !empty( $lp_type ) ){
if( $lp_type == '3' ){
$lp_type = 'Featured';
} elseif( $lp_type == '2' ){
$lp_type = 'Standard';
} elseif( $lp_type == '1' ){
$lp_type = 'Basic';
}
} else{
$lp_type = '';
}
This is the query:
$args = array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => $postsonpage,
'paged' => $paged,
'post__not_in' =>$ad_campaignsIDS,
'tax_query' => $TxQuery,
// 'meta_key' => $MtKey,
// 'orderby' => $lporderby,
// 'order' => $lporders,
'order' => 'DESC',
'orderby' => 'meta_value',
'meta_query' => array(
'relation' => 'OR',
$MtKey,
array(
'relation' => 'OR',
array(
'key' => 'lp_listingpro_options',
'value' => 'lising_type_options',
'compare' => 'LIKE',
),
array(
'key' => 'lp_listingpro_options',
'value' => 'lising_type_options',
'compare' => 'NOT LIKE',
),
),
),
);
Share
Improve this question
asked Oct 16, 2020 at 21:13
PamPam
112 bronze badges
2
|
1 Answer
Reset to default 0Thanks for the response. Managed to find what the cause was. Its caused because of sticky feature, my posts was not sorted accordingly. So, can anyone suggest me how do I remove saved sticky feature field from the database?
Any help would be much appreciated!
本文标签: wp querySort wordpress custom posts based on meta value
版权声明:本文标题:wp query - Sort wordpress custom posts based on meta value 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742054265a2418204.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$MtKey
afterrelation => 'OR'
is throwing me.... ...what is that and why is it there? I don't see anything in your code that defines that variable and I also don't understand why it's dropped in the middle of your query. – Tony Djukic Commented Oct 17, 2020 at 1:33