1

E.g. I have a meta field called car_number:

So, normally I would try something like this to get this value:

$car_value = get_post_meta($post->ID, 'car_number'); 

But the porblem is that I need to get the row only where the car_number is highest e.g. 45. so I need somehow to get the highest value and do something like this:

$post_ID = .. // here I need to get the ID of the post that has the biggest value in the fild car_value

$car_value = get_post_meta($post_ID, 'car_number'); 

how can I get the post_ID of the post that has the highest value (number) in car_value meta field?

3

The WP_Query has a feature called meta_query. Using it allows you to either query by value or numeric value:

$car = new WP_Query( array(
    'post_type'      => array( 'cars' ),
    'meta_key'       => 'car_number',
    'orderby'        => 'meta_value_num',
    'posts_per_page' => 1,
    'order'          => 'DESC',
    'cache_results'  => true,
) );
$carNr = get_post_meta( $car->ID, 'car_number', true );
// Inspect result
var_dump( $carNr );

Now, that we got the right post type, the appropriate meta key and defined that we want to order by its value, we only need to limit the result to a single post and have a descending order. As we now got the post, we can easily grab the meta value.

Another option would be to do a plain query:

global $wpdb;

$wpdb->get_var( 
    "SELECT {$wpdb->postmeta}.meta_value
     FROM {$wpdb->postmeta}
     WHERE 1=1
         AND {$wpdb->postmeta}.meta_key = 'car_number'
     ORDER BY ABS( {$wpdb->postmeta}.meta_value ) DESC
     LIMIT 0,1"
);

Depending on how you saved your values - often plugins save numbers/digits/integers as strings - you may need to convert your value during the query. In that case replace the order by part with

CAST( {$wpdb->postmeta}.meta_value as SIGNED INTEGER )

Note: If you don't have negative integers, you can go with UNSIGNED

or

CONVERT( {$wpdb->postmeta}.meta_value, SIGNED INTEGER )
7
  • It's not working. If I print_r $car->ID I get nothing. Btw. what is meta_value_num ? – Derfder Nov 4 '13 at 13:13
  • I am using this in admin not in frontend. Could that be a problem? – Derfder Nov 4 '13 at 13:24
  • What does WHERE 1=1 mean? – Derfder Nov 4 '13 at 13:27
  • For the future: Please add every detail to your question. File an edit and state that you need it in the admin UI, where you need it and why you need it. For the other questions, please consult Google and the Codex. – kaiser Nov 4 '13 at 13:31
  • @Derfder Please read the code in detail: As you can see, I assumed that your post type is named cars. You'll have to alter the code where your setup defers from this example code. – kaiser Nov 4 '13 at 13:32
0

Try this method

function end_meta_value( $end = "max", $meta )
{
    global $wpdb;
    $query = $wpdb->prepare( 
        "SELECT %s( cast( meta_value as UNSIGNED ) ) FROM {$wpdb->postmeta} WHERE meta_key='%s'",
        $end,
        $meta
    );
    return $wpdb->get_var( $query );
}

from here. For me it worked fine.

4
  • Please always properly escape queries. Else doors are wide open for SQL injections. And if you find a question/answer that fits the purpose please leave a comment or a close vote for a duplicate. Thanks. – kaiser Nov 4 '13 at 11:51
  • 1
    Good. I will. Still learning. – user9 Nov 4 '13 at 11:52
  • Could you use my values provided in my question? Thank you. – Derfder Nov 4 '13 at 13:22
  • I am using this in admin. Could taht be a problem? – Derfder Nov 4 '13 at 13:24

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.