Tag Info

Hot answers tagged

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


12

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


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


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


7

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

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

$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

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

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

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


3

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', ) ) ); ...


3

This should do the trick for you. This shows what template file is stored in post_meta, if one has been selected in the admin panel: $template_name = get_post_meta( $the_query->post->ID, '_wp_page_template', true ); If you want to see if the page is the homepage, use is_home() or is_front_page(). If you want to see what files are generating the ...


3

This question probably belongs to Code Review. Anyway, you can fill your event properties inside an array, which you can iterate: $events = array( 'opening' => array( 'time' => '...time here...', 'formatted_time' => '...time here...', 'label' => __('Opening:'), ...


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


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

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


3

Sorry, doesn't work that way. The "simple" meta parameters are converted into part of the meta_query as a whole. So it's not a separate thing, and your OR relation applies to it as well. Your query thus is not possible with the normal query system, because you want to mix ANDs with ORs (you want items that have event_data ANDed with a bunch of others with ...


3

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


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

How about this: $my_query = new WP_Query(array( 'post_type'=> 'custom_type', 'post_status' => 'publish', 'meta_key' => 'my_custom_field', 'meta_value' => 'custom field value' )); ...


3

Take a look at the codex for a better understanding of queries of custom fields but it should look something like this: $subjects_array = explode("_", $_GET["subjects"]); $args = array( 'post_status' => 'publish', 'post_type' => 'any', 'meta_query' => array( array( 'key' => 'field_name', 'value' ...


3

That's because 'meta_key' and 'meta_value' are not public query vars. In other words, you can't use them in URLs directly, nor should you. Instead, register a specific query var, like so: function register_my_qv() { global $wp; $wp->add_query_var( 'my_qv' ); } add_action( 'init', 'register_my_qv' ); Then, you can go to a URL like this: ...


3

Answer taken from here: meta_query: using BETWEEN with floats and/or casting to DECIMAL add_filter('get_meta_sql','cast_decimal_precision'); function cast_decimal_precision( $array ) { $array['where'] = str_replace('DECIMAL','DECIMAL(5,2)',$array['where']); return $array; }


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

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


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



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