23

Thank you everyone for your help! In the end the query below got me the results I desired - which was to show and sort the posts by a custom field of "publication_date" first - sorting by the date, and if there were multiple of the same date (say, 4 marked June 2013), it would sort those by title. Then, after it runs through all the posts that have the ...


16

This is an old question, but it seems Wordpress has fixed this "missing feature": now, according to Wordpress Codex is possible to check for existence (or non-existence) of the meta key, like this 'meta_query' => array( array( 'key' => 'featured_image', 'compare' => 'EXISTS', //or "NOT EXISTS", for non-existance of this key )...


10

Few years later, the code posted by CSSGirl wasn't working for me because there were some posts that didn't have the meta key or the meta key was empty so this is what I had to do to have all the posts ordered by date and show the ones with a meta key value display first: $args = array( 'post_type' => $type, 'post_status' => 'publish', '...


9

Inserting boolean term meta values When we add non-existent term meta with e.g. add_term_meta( 123, 'test', true ); then we are actually running the following insert : $wpdb->insert( 'wp_termmeta', array( 'term_id' => 123, 'meta_key' => 'test', 'meta_value' => true ) ); within the general add_metadata() function. Now wpdb::insert() ...


7

You can run a loop through all your posts and change their meta value. To do so, create a plugin and run a loop upon its activation, using get_posts(): <?php /* Plugin Name: Update MetaData for Posts Description: Enable this plugin to update the metadata for all the posts Author: JackJohansson Version: 1.0 Author URI: http://example.com */ // Run the ...


6

This is the query that worked for me. Very similar to the comparison in t31os's answer from 2011, but because the meta key/value is just a simple textstring, it doesn't need to be a meta_query array. $args = array( 'posts_per_page' => 5,//replaced 'showposts' in version 2.1 'meta_key' => 'featured_image', 'meta_value' => array(''), '...


6

For a left-sided match you can get around the automatic adding of '%''s by WP by using regular expression RLIKE: 'meta_value' => '^' . preg_quote( $today ), 'meta_compare' => 'RLIKE' Similarly for right-sided match: 'meta_value' => preg_quote( $today ) . '$', 'meta_compare' => 'RLIKE'


5

A meta_query is an array of arrays. Look at the examples in the the Codex. $args = array( 'post_type' => 'my_custom_post_type', 'meta_key' => 'age', 'orderby' => 'meta_value_num', 'order' => 'ASC', 'meta_query' => array( array( 'key' => 'age', 'value' => array(3, 4), 'compare' =>...


5

You are facing an XY Problem. The only reason to save meta values each as a single row, would be if you need them to be searchable. If you need specific sanitization rules, use either filter_var_array() or sanitize the values, then build the array and finally save the value. Gist with a comprehensive filter_var_array() example here.


5

I have solved my problem and it had nothing to do with the query, the database or the post type. The problem was that in trying to initially construct the query I had left a pre_get_posts function in my functions.php which was conflicting with my query. The lesson here for me is to make sure I have thoroughly checked ALL files. The offending counter code: ...


4

The get_pages function is rather old and really shouldn't be used anymore. Use a get_posts instead (a Page is just a special type of Post): $listingPages = get_posts( array( 'meta_key' => 'streetAdd', 'post_type' => 'page', 'post_status' => 'publish', 'orderby' => 'meta_value', 'order' => 'ASC', ...


4

An IN query is not a string comparison query. It is more like a bunch of OR statements. ... WHERE tablename.animal IN ('cat','dog','ferret') is going to be the same as ...WHERE tablename.animal = 'cat' OR tablename.animal = 'dog' OR tablename.animal = 'ferret' $_POST['_casestudypost'] is going to be an array, and if you stored it as a single ...


4

Add this to your functions.php in your template folder. <?php //Set Default Meta Value function set_default_meta_new_post($post_ID){ $current_field_value = get_post_meta($post_ID,'YOURMETAKEY',true); //change YOUMETAKEY to a default $default_meta = '100'; //set default value if ($current_field_value == '' && !wp_is_post_revision($post_ID)){ ...


4

The goal was to paginate comments, showing comments with the highest comment_rating meta value first. Thanks to the answer of cjbj and the comments by Milo and birgire I figured out the rather unintuitive solution to the issue. By using numberand offset I was able to make the results in the proper order, but pagination was strange, and each 5 posts were ...


3

I'm afraid WP_Query is not able to fetch (Posts with custom field) or (ailec_even). You'll have to query the firs one, than the second one and merge those arrays. Use WP_Query instead of query_posts. I've just tried it on my local installation and this code called from index.php of twentytwelve works (brings post with title "Hello post" and ai1ec_event with ...


3

This line is wrong: 'meta_key' => 'adminscore' + 'user_like' + '_count-views_all' + 'comment_count', 'meta_key' only accepts string value. What you need is an additional meta key, e.g. "totalscore" that holds the total value of the scores you have for all the meta values. You can manually run a php function that calculates and store the totalscore ...


3

Am i missing something? <?php $args = array( 'post_type' => 'post', 'posts_per_page' => -1, 'meta_key' => "featured_image" ); $the_query = new WP_Query( $args ); ?> Won't that do it?


3

I modified the original function and KDM's solution to have a more universal function. It goes like this: 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-&...


3

Use a meta_query and set type to NUMERIC- $query->set('meta_query', array( array( 'key' => '_ai_price_int', 'value' => array(0, 4000), 'compare' => 'BETWEEN', 'type' => 'NUMERIC' ) )); $query->set('meta_key', '_ai_price_int'); $query->set('orderby', 'meta_value_num');


3

You have to use the posts_orderby filter to do this at the mo, eg function wpse159469_posts_orderby( $orderby, $query ) { return implode( ' DESC,', explode( ',', str_replace( array(' ASC', ' DESC' ), '', $orderby ) ) ) . ' DESC'; } then around your query: add_filter( 'posts_orderby', 'wpse159469_posts_orderby', 10, 2 ); $query = new WP_Query( array( //...


3

Your syntax is complete wrong in your meta_query. A key/value pair in an array should be in the format of 'key' => 'value'. Your format is 'key', 'value' which in context of an array translate to 0 => 'key', 1 => 'value'. That is why your meta_query does not work. Your failure is also due to not very good housekeeping. As your meta_query is ...


3

You forgot option 3 - Add a special table in which the pair (user,post id) will be the index. Ok I am not a MySQL person so maybe it is too extreme, but maybe having two tables one with users as index and one with posts will be even better. The thing about performance is that there are rarely absolute solutions for everybody at anytime, and the "best" ...


2

There's wp_list_pluck() to get certain fields out of arrays or objects: $object = new StdClass(); $object->foo = 'Hi!'; $object->bar = 'Yo!'; $array['foo'] = 'Hi!'; $array['bar'] = 'Yo!'; $result_obj = wp_list_pluck( $object, 'foo' ); var_dump( $result_obj ); // Result: string (3) Hi! $result_array = wp_list_pluck( $array, 'bar' ); var_dump( $...


2

You could do a custom SQL query: global $wpdb; $query = "SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = 'director' ORDER BY meta_value;"; $directors = $wpdb->get_results( $query ); foreach( $directors as $director ) : echo $director->meta_value; endforeach; one thing to note here though is that since you're not querying the ...


2

You might want to check out the query improvements in WP 4.2 for ‘orderby’ and ‘meta_query’. Details are on https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query. Try to name your meta_query clauses, then order by them. Following code is untested: $query = array( 'order' => 'DESC', 'meta_query' => array( ...


2

Looking at the Codex entry for get_comment_meta(), it appears that when the $single argument is set to TRUE (as you have done), the function returns a string. Try throwing an echo() into the works: <?php echo get_comment_meta( $comment->comment_ID, 'country', true ); ?>


2

You are using get_post_meta incorrectly, so it is returning FALSE every time. As PHP equates '0' and FALSE, your div will always be displayed. The correct syntax for get_post_meta is: <?php $meta_values = get_post_meta($post_id, $key, $single); ?> where $post_id is the only required argument.


2

Taxonomies and meta data (Custom Fields) are two different things. If you want to filter on a taxonomy term, you want a tax_query, not a meta query.


2

It sounds like you are trying to directly save the altered information to the serialized value in the database. You can't do that. You have to extract the current meta data for that key so that you have and array or object, update that array or object, and then save the whole thing back to the database. Something like... $old_meta = get_post_meta( $pId, '...


2

Is there any reason you couldn't enforce the publication_date meta key to exist for every post just with an empty value? So in your save_post action you would add/update the meta key whether the $_POST value is empty or not. You would have to run an update script to loop over your older posts and add the key with an empty value eg: add_action( 'admin_init'...


Only top voted, non community-wiki answers of a minimum length are eligible