Query_posts with custom field meta value

The best solution i found for querying posts where the meta_key IS NOT or doesn’t exists is to use the posts_where filter with a small sub-query, try this: //paste this function in your theme’s functions.php function metakey_no_featured( $where ) { global $wp_query; global $wpdb; $where .= $wpdb->prepare(” AND $wpdb->posts.ID NOT IN ( SELECT post_id FROM … Read more

How can I do a variable for meta_query?

Use a ternary operation to keep it trim. array( ‘key’ => ‘rob_value’, ‘value’ => $rob, ‘compare’ => ( empty( $rob ) ? ‘!=’ : ‘=’ ) ); Breakdown, incase you’ve not seen ternary operators before.. array( ‘key’ => ‘rob_value’, ‘value’ => $rob, ‘compare’ => ( // If empty( $rob ) // Then ? ‘!=’ // … Read more

Plot location on Google map using full address specified in custom field

Dean, your best bet is to do it through JS on the front end. I use the following function to covert the postcode to a Lat and Lng: function codeAddress(address){ geocoder.geocode({ ‘address’: address }, function(results, status){ if(status == google.maps.GeocoderStatus.OK){ map.setCenter(results[0].geometry.location); map.setZoom(16); var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location }); markersArray.push(marker); // BA Lng, … Read more

Efficiently sort only certain categories by custom field

I would suggest to hook onto pre_get_posts and alter the query, rather than firing off your own in the template (it keeps your template tidy, and you’ll avoid doubling your database queries!). Below demonstrates this, plus a bonus settings field in the admin (under “Reading”) to select the categories that “special sorting” applies to – … Read more

How Do I Display A Custom Field In The Sidebar Of All My Pages?

I checked the code in the repository, and this specific function doesn’t have an ID parameter, meaning that it only grabs the CF of the current post/page being displayed. A fast solution would be: $my_page = get_page_by_path( ‘my-page’ ); $meta_values = get_post_meta($my_page->ID, ‘custom-field-name’, true); Maybe the plugin has another function for that, but you’d better … Read more

Get post ids sorted by meta_key

It’s easy. 🙂 You would use get_posts function. See WP_Query class for complete list of parameters that you can use. function get_posts_by_view_count() { $ids = array(); $args = array( ‘orderby’ => ‘meta_value_num’, ‘order’ => ‘DESC’, ‘meta_key’ => ‘post_view_count’ ); $posts = get_posts( $args ); if( $posts ) { foreach( $posts as $post ) { $ids[] … Read more