Search
Close this search box.

WordPress meta query: Get lowest or highest number value?

Amitpal Singh
Amitpal Singh
November 23, 2021
function lowest_price(){

global $wp_query;

$args = array(
  'post_type' => array("automjete", "elektronike", "vendbanime", "sport-rekreacion", "kafshe", "telefona", "shtepi-kopsht", "veshje-bukurie", "biznes-sherbime"),  
  'orderby' =>  'meta_value', // meta_value_num for value
  'order' => 'ASC', // or DSC for low/high
  'posts_per_page' => 1,
  'meta_query'        => array(
            array(
                'key' => 'meta-box-price',
                'type' => 'UNSIGNED'
            )
        )
);

$wp_query = new WP_Query( $args );

if ( $wp_query->have_posts() ) {
  while ( $wp_query->have_posts() ) {
    $wp_query->the_post(); 

     return get_post_meta(get_the_ID(), "meta-box-price", true);

  }
}
	
} 
function highest_price(){

global $wp_query;

$args = array(
  'post_type' => array("automjete", "elektronike", "vendbanime", "sport-rekreacion", "kafshe", "telefona", "shtepi-kopsht", "veshje-bukurie", "biznes-sherbime"),  
  'orderby' =>  'meta_value', // meta_value_num for value
  'order' => 'DSC', // or DSC for low/high
  'posts_per_page' => 1,
  'meta_query'        => array(
            array(
                'key' => 'meta-box-price',
                'type' => 'UNSIGNED'
            )
        )
);

$wp_query = new WP_Query( $args );

if ( $wp_query->have_posts() ) {
  while ( $wp_query->have_posts() ) {
    $wp_query->the_post(); 

     return get_post_meta(get_the_ID(), "meta-box-price", true);

  }
}
	
} 

Share this post:

How to Attribute?

Lorem ipsum is typically a corrupted version of De finibus bonorum et malorum, a 1st-century BC text by the Roman statesman and philosopher Cicero.
for Example: Website, Social Media, Blogs, ebooks , newsletter, etc.
Lorem ipsum is typically a corrupted version of De finibus bonorum et malorum, a 1st-century BC text by the Roman statesman and philosopher Cicero.
Copied!

Got a Question? Check out our FAQ Section.

Your action, our appreciation

It encourage us to give you more valuable content on website.