0

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

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.

  • thanks for taking time to answear, but this doesn't help... if you read my question, those ID are relationship ID's, not "post" id's, and in any case, this will not order starting by posts from value 5 and then from others. – Gregory Jun 9 at 17:50
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.