Meta query terribly slow

I’ve come across this problem and it seems MySQL doesn’t deal well with the multiple joins to the same table (wp_postmeta) and OR-ed WHERE that WP generates here. I dealt with it by rewriting the join and where as mentioned in the post you link to – here’s a version that should work in your … Read more

Query WP REST API v2 by multiple meta keys

Adding a custom endpoint is pretty straightforward. I also modified the url to look more like http://example.com/wp-json/namespace/v2/posts?filter[meta_value][month]=12&filter[meta_value][year]=2015 function wp_json_namespace_v2__init() { // create json-api endpoint add_action(‘rest_api_init’, function () { // http://example.com/wp-json/namespace/v2/posts?filter[meta_value][month]=12&filter[meta_value][year]=2015 register_rest_route(‘namespace/v2’, ‘/posts’, array ( ‘methods’ => ‘GET’, ‘callback’ => ‘wp_json_namespace_v2__posts’, ‘permission_callback’ => function (WP_REST_Request $request) { return true; } )); }); // handle the request … Read more

Using meta_query, how can i filter by a custom field and order by another one?

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, … Read more

WP Query Args – Title or Meta Value

Note that the relation part in the meta_query argument, is only used to define the relation between the sub meta queries. You can try this setup: $args = [ ‘_meta_or_title’ => $thesearch, // Our new custom argument! ‘meta_query’ => [ [ ‘key’ => ‘model_name’, ‘value’ => $thesearch, ‘compare’ => ‘like’ ] ], ]; where we … Read more

Filtering multiple custom fields with WP REST API 2

This solution works with get_items() in /lib/endpoints/class-wp-rest-posts-controller.php of the v2 WP Rest API. First, you’ll want to construct the GET arguments like you would for a new WP_Query(). The easiest way to do this is with http_build_query(). $args = array ( ‘filter’ => array ( ‘meta_query’ => array ( ‘relation’ => ‘AND’, array ( ‘key’ … Read more

How to do a meta query using REST-API in WordPress 4.7+?

You can write your own REST handler for custom queries if you want. In your case, the query can done by doing so: // Register a REST route add_action( ‘rest_api_init’, function () { //Path to meta query route register_rest_route( ‘tchernitchenko/v2’, ‘/my_meta_query/’, array( ‘methods’ => ‘GET’, ‘callback’ => ‘custom_meta_query’ ) ); }); // Do the actual … Read more

meta_query ‘compare’ => ‘IN’ not working

There’s no easy way to search serialized values in a meta query. If the list of values isn’t crazy long, potentially you could set up multiple meta queries: ‘meta_query’ => array( ‘relation’ => ‘OR’, array( ‘key’ => ‘system_power_supply’, ‘value’ => ‘single’, ‘compare’ => ‘LIKE’, ), array( ‘key’ => ‘system_power_supply’, ‘value’ => ‘redundant’, ‘compare’ => ‘LIKE’, … Read more

How can I create a meta_query with an array as meta_field?

Feeding the query an array of possible values If the value in the database is a string and you want to feed the query several values: $args = array( ‘post_type’ => ‘news’, ‘meta_query’ => array( array( ‘key’ => ‘topics’, ‘value’ => array ( ‘sports’, ‘nonprofit’, ‘community’ ), ‘compare’ => ‘IN’ ) ) ); Searching for … Read more

Nested meta_query with multiple relation keys

The question was for WordPress 3.0, but just in case someone has the same question for a more recent version, from WordPress Codex: “Starting with version 4.1, meta_query clauses can be nested in order to construct complex queries.” https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters So, that query should work on the current WordPress version.