wp query – How to display posts from a specific term first then another posts from another terms using tax_query

Question

I am building a Custom query to display custom posts ( Jobs post type ) from a specific taxonomy ( job location taxonomy ) depending on IP detection. But i need to query all jobs and display them having those country ( The IP detected country) related to be on the beginning.

The following query only gets country related jobs, but how to extend this to display detected country’s posts 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;

0
Mohamed Omar 3 weeks 2022-11-08T03:59:22-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse