advanced custom fields - wp query multiple values > display a specific value first

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

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 badges
Add a comment  | 

2 Answers 2

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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745415317a4626721.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信