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

Get a single post ID based on an exact match of 1 or more meta values

If you want to match them all, you have to add ‘relation’ => ‘AND’ and check for each value separately: $args = array( ‘post_type’ => ‘post’, ‘meta_query’ => array( ‘relation’ => ‘AND’, array( ‘key’ => ‘_some_meta_key’, ‘value’ => 1, ‘compare’ => ‘=’ ), array( ‘key’ => ‘_some_meta_key’, ‘value’ => 2, ‘compare’ => ‘=’ ), array( … Read more

Ordering a mysql style datetime stamp with meta_query

‘orderby’ => ‘meta_value_num’ will try to force the value to a number by adding 0. In other words, “2013-09-30 22:00:00″+0 When I tested that both PHP and MySQL converted the string to 2013, which is not what you want. That particular date format, and only that date format, will sort correctly if you just leave … Read more

WP Query – Is this correct?

I am guessing but based on your choice of placeholder names in the code above… $args = array( ‘meta_key’ => ‘myvalue’, ‘orderby’ => ‘meta_value_num’, ‘order’ => ‘DESC’ ); … your meta_key argument is wrong. That is the meta key not the meta value as your naming convention suggests. meta_key (string) – Custom field key. http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters … Read more

get posts where a custom field contains a text

Here are few ideas: 1) You could try to compare with LIKE instead: ‘key’ => ‘_my_custom_field_key’, ‘value’ => ‘test1’, ‘compare’ => ‘LIKE’ It should give you a SQL query with: … LIKE ‘%test1%’ … 2) You might want to save the string as ,test1,test2,test3, to make the whole word search easier: ‘key’ => ‘_my_custom_field_key’, ‘value’ … Read more

get_users meta_query: REGEXP not working for matching new lines

I found the problem and also fixed a bug in the regex. Final code: ‘meta_query’ => array( array( ‘key’ => ‘my_meta_field’, ‘value’ => “(^|\n|\r\n)99999($|\n|\r\n)”, ‘compare’ => ‘REGEXP’, ), ), The problem here was WordPress was adding extra backslashes on the special characters. By wrapping the expression in double quotes, this problem was solved. Also added … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)