Skip to content

Instantly share code, notes, and snippets.

@mujahidi
Created May 25, 2018 21:58
Show Gist options
  • Save mujahidi/d12c40536e389d972ee810b5077df798 to your computer and use it in GitHub Desktop.
Save mujahidi/d12c40536e389d972ee810b5077df798 to your computer and use it in GitHub Desktop.
Get the highest value from WordPress post meta
<?php
// WordPress saves post meta values as 'longtext' type
// and hence, it makes it impossible to fetch the highest value based on a number.
// So I came up with the following solution to get the highest value from the post meta
// CHANGE [YOUR_META_KEY] with yours post meta key
global $wpdb;
$highest_val = $wpdb->get_var( "SELECT
meta_value
FROM
wp_postmeta
WHERE
meta_key = '[YOUR_META_KEY]'
ORDER BY
CAST(meta_value AS UNSIGNED) DESC"
);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment