wp query – How to display post before a specific word then post from other posts using tax_query?

I am creating a custom query to display custom posts (jobs post types) from a specific taxonomy (job location taxonomy) based on IP identity. But I need to query all the jobs and display those country (the country the IP is traced from) being in the beginning.

The following query gets only country related jobs, but how to extend it to detect country’s post first

    $args = array(
        'post_type'      => 'noo_job',
        'post_status'    => 'publish',
        'posts_per_page' => 9,
    );

// anony_ip_info is a custom funtion to get the country name
$user_country = anony_ip_info(null, 'country');


if( $user_country && !is_null($user_country) ) 
{
    // Now get terms that might match detected country name
   $term_args = array(
        'taxonomy'               => 'job_location',
        'hide_empty'             => false,
        'fields'                 => 'id=>name',
        'name__like'             => $user_country,
    );

    $get_term = new WP_Term_Query($term_args);

    if ( !empty($get_term->terms && is_array( $get_term->terms )) ) 
    {
        // If there multiple match get the first
        $user_country_data = noo_array_1st_element($get_term->terms);

        $term_id = array_keys( $user_country_data )[0];

        $term_name = array_shift($user_country_data);

        $args['tax_query'] = array( 
            array(
                'taxonomy' => 'job_location',
                'terms'    => array(apply_filters( 'wpml_object_id', $term_id, 'job_location', true ))
            )
        );
    }
}
$query = new WP_Query( $args );
if ( $query->have_posts() ):

endif;

Leave a Comment