Tag Info

Hot answers tagged

15

The first several work about like you would expect: = equals != does not equal > greater than >= greater than or equal to < less than <= less than or equal to LIKE and NOT LIKE are SQL operators that let you add in wild-card symbols, so you could have a meta query that looks like this: array( 'meta_key' => 'name', ...


13

No, it is not possible. I strongly recommend you unserialise your data and modify your save routine. Something similar to this should convert your data to the new format: $args = array( 'post_type' => 'my-post-type', 'meta_key' => '_coordinates', 'posts_per_page' => -1 ); $query = new WP_Query( $args ); if($query->have_posts()){ ...


10

The meta_value is not of an integer type for max to return proper values. You can use mysql cast method to convert into integers as follows: SELECT max(cast(meta_value as unsigned)) FROM wp_postmeta WHERE meta_key='price'


9

First, just pass your arguments to the constructor of WP_Query as this is both cleaner, and the way you're supposed to do it according to the Codex documentation of the class. You should be constructing things like this: $my_key_query_args = array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'meta_query' => array( ...


9

This seems to be a bug in Wordpress. Wordpress actually modifies the meta_query if you specify orderby and meta_key as query vars. Normally this modification adds the new meta_key as the first array in meta_query array and hence the orderby is applied to the first meta key specified in meta_query. But when you modify orderby, meta_key and meta_value ...


8

First, your date format has to be in descending order from largest to smallest units, i.e.: year, month, day, hour, minute, second, etc., otherwise MySQL can't query or order on the field. In this example I use year - month - day: $today = date( 'Y-m-d' ); $args = array( 'post_type' => 'vehicle', 'meta_query' => array( array( ...


7

Getting posts without a certain meta key is a little tricky, namely due to the database design and the nature of SQL joins. AFAIK, the most efficient way would be to actually grab the post IDs that do have the meta key, and then exclude them from your query. // get all post IDs that *have* 'meta_key' with a non-empty value $posts = $wpdb->get_col( ...


6

That seems to be impossible. Please someone correct me if I'm wrong. The meta_query parameter will actually be transformed into a WP_Meta_Query object, and the relation verification won't go deeper in wp-includes/meta.php, and occurs just once in the top level: if ( isset( $meta_query['relation'] ) && strtoupper( $meta_query['relation'] ) == 'OR' ) ...


6

You really are going to lose the ability to query your data in any efficient manner when serializing entries into the WP database. The overall performance saving and gain you think you are achieving by serialization is not going to be noticeable to any major extent. You might obtain a slightly smaller database size but the cost of SQL transactions is going ...


5

Instead of trying to display all the matching events on your by-date page, you could try to display through ?post_type=event like this: function rewrite_rule_by_date() { add_rewrite_rule('by\-date/([0-9]{4}\-[0-9]{2}\-[0-9]{2})$', 'index.php?post_type=event&event_date=$matches[1]', 'top'); } add_action( 'init', 'rewrite_rule_by_date' ); function ...


4

You're getting all the posts because you set 'meta_key' => 'event_date'. This is equivalent to adding another subquery into 'meta_query' that says "get me all posts which have an event_date custom field, with any value OR that have even_date to $today etc.". PS: There are many event plugins out there that have already figured all this stuff out. Give ...


4

If you want to have more control over the filtering of your posts then separating the meta values into separate keys is the best option to go about it. Also what you could do is, only the values that would be required while filtering could be separated and the rest of them could go into a serialized array.


4

For whats its worth, use wordpress current_time() for this sort of queries. current_time('mysql') gives server time according to admin settings that is acceptable by MySQL


4

$wpdb->wp_postmeta is wrong. Always use just $wpdb->postmeta – without further prefixes. So … global $wpdb; $querystr = " SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE 'meta_key' LIKE 'movie_name' ORDER BY meta_value ASC"; $movie_names = $wpdb->get_results($querystr, OBJECT); if ( ! $movie_names ) { ...


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 ...


4

Update - it may be supported in a meta_query. I need more information - see below. You can't do it using a meta_query, it's not supported. Do you have data in meta_data fields? - WordPress queries will exclude posts where any of the orderby (or meta_query) fields is missing. WordPress adds a join condition to the SQL query to support the orderby or ...


4

Note that when using a meta_compare value of 'LIKE', WordPress automatically wraps the wildcard character ( % ) around the meta_value string. So the 'Pat%' example could fail to return any results.


4

Check the Codex. meta_query accepts an EXISTS comparison. $args = array( 'post_type' => 'somepostype', 'meta_query' => array( array( 'key' => 'fname'.$userid, 'compare' => 'EXISTS', ), array( 'key' => 'lname'.$userid, 'compare' => 'EXISTS', ) ) ); ...


4

A meta_query is an array of arrays. You only have an array. $query->set('meta_query', array( 'key' => 'shru_price', 'value' => $_GET['minPrice'], 'compare' => '>=', 'type' => 'NUMERIC' )); It should be: $query->set( 'meta_query', array( array( 'key' => 'shru_price', ...


4

There are two parts to the solution here. You need to use a JSON API custom controller In your custom controller, you'll need to decide how to pass the meta_query data structure Depending on how robust you need this to be, you could use a variety of approaches. Here is the maximalist approach, that will allow any kind of meta_query, encoding the ...


3

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( ...


3

Problem is that meta values are not unique key/value data. E.g. with the key 'OneKey' you can have a lot of values for the same post. So, in a flat table, for the column 'OneKey' you should have multiple rows where the post id is the same and seems to me is not what you want. Sure, you can take ony one value treating all meta key as singular, but also a ...


3

There are different way to do the trick, 2 comes into my mind: use a complete custom $wpdb query use WP_Query with filters, using WP_Meta_Query to build the additional sql I'll post here sample code for case #2 /** * Run on pre_get_posts and if on home page (look at url) * add posts_where, posts_join and pre_get_posts hooks */ function ...


3

Using your constructed array, I think this is the correct syntax: $meta_query_args = array( 'meta_key' => 'price', 'meta_value_num' => 1000, 'meta_compare' => '<=', 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'roomcount', 'value' => 3, 'type' => ...


3

The culprit The culprit of the matter is meta queries not supporting different and/or nested relations - a shortcoming by the way, that has driven me nuts before as well. In a recent instance with a search scenario also. What you want to do simply cannot be accomplished with WP_Query as well as one loop alone.As you appear to have noticed, whether you ...


3

pre_get_posts triggers for every query (front and admin side). It also triggers when querying nav-menu items (which is what is happening here). Most of the time you only want to filter particular queries - so you need to check if that query is one you wish to modify. Usually - and I think your example might be one - you only want to modify the 'main ...


3

That massive function was a bit scary, I got this working like so - with two arguments (that exclude the features) $args = array( 'meta_query' => array( 'relation' => 'OR', array( // new and edited posts 'key' => 'Set as Featured Post', 'compare' => '!=', 'value' => 1 ...


3

First of all post field of WP_Query is current post ID and not post object. But I don't think you should use it before calling the_post() method. Normally you should do it in this way: $args = ... $hometeams = new WP_Query( $args ); $teamishome = $hometeams->have_posts(); while ( $hometeams->have_posts() ) { $hometeams->the_post(); $scorehome ...


3

I did something similar for a client a while back, I'll give you some of the code here as-is that you can possibly adapt to your needs. I'll warn you, it's quite a bit to parse through! First, I set up some custom rewrite rules to get the year/month URL structure and some query vars to pass the year and month to my template. In this example I have a page ...


3

Just a plugin concept … Add a form with a button to each user profile, post or wherever you want it named Follow or Unfollow. Show the button only if is_user_logged_in(). You may use a widget for the form. On form submit update a user meta named follows for the user who clicked the button and another one named followers for … well … the user who just got a ...



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