How to get the db table row that has the biggest value in the meta value?

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 … Read more

Sorting posts by custom fields in meta_query

As of WordPress 4.2, you can combine meta_query and orderby. The following example is taken from make.wordpress.org. Example of a simple meta_query: $q = new WP_Query( array( ‘meta_query’ => array( ‘relation’ => ‘AND’, ‘state_clause’ => array( ‘key’ => ‘state’, ‘value’ => ‘Wisconsin’, ), ‘city_clause’ => array( ‘key’ => ‘city’, ‘compare’ => ‘EXISTS’, ), ), ‘orderby’ … Read more

Escaping Quotes

WordPress emulates magic_quotes_gpc and won’t be deprecating this for some time due to concerns over plugin security and backward compatibility. To work around this, you need to use stripslashes() or, if you need to strip slashes when data arrives via $_POST, $_GET, $_COOKIE, and $_REQUEST arrays, you can use the WordPress stripslashes_deep() function: http://codex.wordpress.org/Function_Reference/stripslashes_deep

How do I exclude posts by custom field value?

You can use meta query in wp query to get required posts $args = array( ‘meta_query’ => array( ‘relation’ => ‘OR’, array( ‘key’ => ‘city’, ‘value’ => ‘Melbourne’, ‘compare’ => ‘NOT LIKE’ ), array( ‘key’ => ‘city’, ‘value’ => ‘Melbourne’, ‘compare’ => ‘NOT EXISTS’ ) ) ); $query = new WP_Query( $args );