admin管理员组文章数量:1426120
I have a query that get relationship fields for multiple ID.
I get the ID of selected item, and merge it in an array with other ID's
ex :
my page relationship ID = 5 my other pages ID in array = 6,3,8 so i make an array = 6,3,8,5
how can I display my value, to display first the value of "5", and then the other values ?
shoud I do a query for
5
and then a query for 6,3,8 ?
or is there a way to order by 5 and then by others ?
My query :
$args = array(
'post_type' => 'maison-hotes',
'post__not_in' => $post_id,
'meta_query' => array(
'relation' => 'OR',
)
);
foreach($cities_id as $single_city_id) {
$args['meta_query'][] = array(
'key' => 'contact_city',
'value' => '"'.$single_city_id.'"',
'compare' => 'LIKE'
);
}
$query = new WP_Query($args);
if ($query->have_posts() ) :
echo '<h3 class="widget-title"><span><i class="fas fa-hotel"></i> D\'autres chambres d\'hôtes à '. $city_name .'</span></h3>';
while ( $query->have_posts() ) : $query->the_post();
my_stuf();
endwhile;
endif;
In SQL i know i can do something like
ORDER BY CASE contact_city WHEN '5' THEN 1 ELSE 2
But i don't know how to reproduce this to wordpress
I have a query that get relationship fields for multiple ID.
I get the ID of selected item, and merge it in an array with other ID's
ex :
my page relationship ID = 5 my other pages ID in array = 6,3,8 so i make an array = 6,3,8,5
how can I display my value, to display first the value of "5", and then the other values ?
shoud I do a query for
5
and then a query for 6,3,8 ?
or is there a way to order by 5 and then by others ?
My query :
$args = array(
'post_type' => 'maison-hotes',
'post__not_in' => $post_id,
'meta_query' => array(
'relation' => 'OR',
)
);
foreach($cities_id as $single_city_id) {
$args['meta_query'][] = array(
'key' => 'contact_city',
'value' => '"'.$single_city_id.'"',
'compare' => 'LIKE'
);
}
$query = new WP_Query($args);
if ($query->have_posts() ) :
echo '<h3 class="widget-title"><span><i class="fas fa-hotel"></i> D\'autres chambres d\'hôtes à '. $city_name .'</span></h3>';
while ( $query->have_posts() ) : $query->the_post();
my_stuf();
endwhile;
endif;
In SQL i know i can do something like
ORDER BY CASE contact_city WHEN '5' THEN 1 ELSE 2
But i don't know how to reproduce this to wordpress
Share Improve this question edited Jun 11, 2019 at 8:56 Gregory asked Jun 9, 2019 at 14:18 GregoryGregory 6025 silver badges20 bronze badges2 Answers
Reset to default 0 'orderby' => 'post__in',
'post__in' => array(5,6,3,8),
You need ACF to pass the ID's in order but that should do it.
After a weekend of searching, i have a solution !
function custom_posts_join($join){
global $wpdb;
$join .= " LEFT JOIN $wpdb->postmeta as pm ON $wpdb->posts.ID = pm.post_id ";
return $join;
}
add_filter( 'posts_join' , 'custom_posts_join');
add_filter( 'posts_orderby', 'order_search_by_posttype', 10, 2 );
function order_search_by_posttype( $orderby, $wp_query ) {
global $city_id;
$orderby =
"
CASE WHEN pm.meta_key = 'contact_city' AND pm.meta_value = '$city_id' THEN '1'
ELSE pm.meta_key = 'contact_city' END ASC";
return $orderby;
}
and for shure, don't forget to remove filter after using it
remove_filter('post_join' , 'custom_posts_join');
remove_filter('posts_orderby', 'order_search_by_posttype', 10, 2);
本文标签: advanced custom fieldswp query multiple values gt display a specific value first
版权声明:本文标题:advanced custom fields - wp query multiple values > display a specific value first 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745415316a2657657.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论