Created
May 25, 2018 21:58
-
-
Save mujahidi/d12c40536e389d972ee810b5077df798 to your computer and use it in GitHub Desktop.
Get the highest value from WordPress post meta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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