Skip to main content
Share Your Experience: Take the 2024 Developer Survey

Questions tagged [meta-query]

Refers to a mechanism built into the WP_Query class for querying the database for post data, including pages and CPTs, based upon post meta data.

Filter by
Sorted by
Tagged with
51 votes
14 answers
80k views

meta_query with meta values as serialize arrays

I'm working on a project in which I'm creating a custom post type and custom data entered via meta boxes associated with my custom post type. For whatever reason I decided to code the meta boxes in ...
tollmanz's user avatar
  • 5,510
31 votes
5 answers
83k views

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

Here are the args for my query : $args = array( 'post_type' => 'news', 'meta_query' => array( array( 'key' => 'topics', 'value' => 'sports', ...
mike23's user avatar
  • 5,981
11 votes
3 answers
21k views

WP Query Args - Title or Meta Value

How do I query by meta_value or title? If I set meta_values, $args['meta_query'] = array( 'relation' => 'OR', array( 'key' => 'model_name', 'value' => $thesearch, ...
Bryan's user avatar
  • 281
63 votes
2 answers
176k views

Meta_query compare operator explanation

I noticed that there are bunch of operator can be use for compare in meta_query. However, I am not quite sure what operator I should use, it is somehow confusing like = and LIKE operator. I would ...
dev-jim's user avatar
  • 1,978
18 votes
5 answers
16k views

Filtering multiple custom fields with WP REST API 2

I want to filter posts based on multiple acf custom fields with AND relation. Something like this: $args = array( 'post_type' => 'product', 'meta_query' => array( '...
Sohrab Taee's user avatar
40 votes
13 answers
103k views

Using meta query ('meta_query') with a search query ('s')

Trying to build a search that not only searches the defaults (title, content etc) but also a specific custom field. My current query: $args = array( 'post_type' => 'post', 's' => $query, ...
luke's user avatar
  • 649
2 votes
2 answers
8k views

filter search result with custom post type meta key

The default search returns all post types, which is what I want. But for one specific custom post type I want to limit the result to a specific meta key value (a custom date that must be greater than ...
Sjeiti's user avatar
  • 171
17 votes
3 answers
35k views

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

I am trying to filter by a meta query but no matter what I try I can not make it work. Basically I want this query in through the rest API: wp-json/wp/v2/books?meta_query[relation]=OR&meta_query[...
Tchernitchenko's user avatar
11 votes
1 answer
15k views

Meta query with string starting like pattern

I have a custom WP_Query with the following meta_query: $meta_query = array(); $meta_query['relation'] = 'AND'; if(!empty($postcode)) { $meta_query[] = array( 'key' => 'postcode', ...
Andy's user avatar
  • 1,027
10 votes
2 answers
9k views

Query WP REST API v2 by multiple meta keys

Using v2 of the REST API, I'm wanting to query some posts by multiple meta keys. With v1 I was able to format the url like &filter[meta_value][month]=12&[meta_value][year]=2015 and it worked (...
Trevan Hetzel's user avatar
8 votes
3 answers
11k views

How to query posts based on lat-lng coordinate as post meta?

I'm planning a custom WordPress theme where the Custom Post Type (CPT) will have latitude and longitude coordinate as it's meta value. The latitude and longitude will be displayed as a Marker in a ...
ifdion's user avatar
  • 1,467
31 votes
3 answers
90k views

Nested meta_query with multiple relation keys

I am curious whether Wordpress is able to run nested meta_query, with each having different relation keys? As of Wordpress 3.0, tax_query is able to perform this function; I'm wondering whether this ...
Andrew Odri's user avatar
8 votes
2 answers
8k views

Sorting: custom query with orderby meta_value_num THEN by title

i try to run a custom post type query to match following criteria: sort movies first by year in descending order, after that ("inside" the year order) by title alphabetically. desired output: ...
kyii's user avatar
  • 83
5 votes
1 answer
11k views

WP_Query with checkbox meta_query

I have posted this on StackOverflow too, not sure if that's allowed - if not let me know and I'll remove one (I don't want to look like I'm spamming). Anyway.... I have a custom post type and have ...
Adam Moss's user avatar
  • 1,052
5 votes
2 answers
2k views

WP_Query orderby custom field then post_date in one query

I need to order the first three posts by a custom field, and subsequent posts by post_date in one query - for example: Post 4 (field_order: 1) Post 3 (field_order: 2) Post 2 (field_order: 3) Post 1 (...
londonfed's user avatar
  • 153
5 votes
1 answer
7k views

Meta Query with AND & OR?

I'm trying to do some filtering for some posts in WP_Query, but I can't seem to get the right condition. Basically what I wanna do, is get posts that have the custom field event_data (sorted by this ...
Olsi's user avatar
  • 173
3 votes
4 answers
39k views

LIKE %...% Meta Query

I have a serialized array as a meta value and I'm trying to run WP Query where it finds a specific value in the serialized array. Here's an example of the serialized array field: a:6:{i:0;s:3:"173";...
Troy's user avatar
  • 237
3 votes
2 answers
18k views

meta_query with multiple values

I need your help. There is my problem: I have custom post type "Player". And this CPT has custom field "Seasons". Seasons can have multiple values separated by comma - for example "2014,2015,2016" ...
libor's user avatar
  • 31
3 votes
1 answer
16k views

WP_Query using meta_query with relation OR and orderby meta_value doesn't work

I've been bashing against a bug for a few hours now, and can't seem to find my error. Could there be a bug with the relation type 'OR' in WordPress? $evenements = new WP_Query(array( 'post_type' ...
Fredy31's user avatar
  • 858
2 votes
1 answer
2k views

How do I create my own nested meta_query using posts_where / posts_join?

Some of my posts (not all) have a price as a meta key/value. Today I use the pre_get_posts action so that my users can search for prices that are between a certain value. This is the code that I'm ...
PappApp's user avatar
  • 23
2 votes
1 answer
3k views

order by meta_value serialized array

Hi I'm trying to pull posts using the wp query. Just one problem--I am trying to sort the posts alphabetically by a certain meta_value. Thing is that this meta_value is a serialized array. Is there an ...
KT.'s user avatar
  • 153
2 votes
2 answers
3k views

I can't set meta_key in my custom post type query

I've created a custom post, events, and saved the meta data event_year to each event. I'm trying to get a year's event archive. When I go the the following URL, the meta_key and meta_value are not ...
Matt's user avatar
  • 465
2 votes
2 answers
3k views

WP_Query not working as expected for attachments and custom meta_query

If I use get_posts() like so I get a number of results with the value 1 for the my_key meta_key: $posts = get_posts( array( 'post_type' => 'attachment', 'meta_key' => '...
Mike's user avatar
  • 813
1 vote
1 answer
2k views

Meta Query "IN" doesn't work with ACF checkbox filter

I try to integrate to my CPT archive page some ACF filters. I start with this tutorial : https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/ It works when I use it ...
Nicolas Lorand's user avatar
1 vote
2 answers
1k views

Multiple Values stored as array in Meta Query

I'm a little confused as to best practise for saving multi-select values in meta fields. Assuming <select multiple name="_casestudypost[]"> is my form field if I store $_POST['_casestudypost'...
Squiggs.'s user avatar
  • 123
1 vote
1 answer
1k views

Order by nested named meta queries with 'exists' and 'not exists'

Each of my posts (online store items) has meta keys: tdlrm_mp - my own menu_position (goes from 1 up and, unlike the wp_posts menu_position, may not exist) 1C_quantity_total - number of items in ...
Artem's user avatar
  • 305
0 votes
1 answer
2k views

datetime picker, timestamps and meta queries

I have a custom meta box with a datepicker. The datepicker outputs 2013-01-17 10:00 in my database for an event that is 01-17-2013 at 10pm PST. $eventlist = new WP_Query(array( 'post_type' => '...
Michael Thomas's user avatar
0 votes
2 answers
7k views

Plugin that would allow WordPress Authors to "follow" other Authors and query "Activity Info" from that

I have a WordPress blog that is all user based Authors that submit content and such. Currently in the site, you can view other authors profiles but basically just view their content from the author....
Zach Reed's user avatar
  • 173
-1 votes
1 answer
132 views

Documentation for post meta

Trying to solve this problem I tried to find the complete list of all available/possible default "post meta" tags/entries. I couldn't find this info documented anywhere. Any pointers?
IXN's user avatar
  • 916
11 votes
5 answers
16k views

Meta query terribly slow

I have a custom meta query which is terribly slow or doesn’t even load till the end. With up to three arrays in 'meta_query' the query works fine, with four and up it doesn’t work anymore. When ...
user1706680's user avatar
10 votes
1 answer
34k views

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

With the following code (in functions.php) my posts (of CPT event) are ordered by _end_date instead of _start_date. What's the proper solution to this as of WP 3.1.3? Of course I'd like to avoid using ...
user6254's user avatar
  • 125
9 votes
4 answers
2k views

Set Alias for meta_query arguments in get_posts()

Is there a way to set an alias on the meta_query arguments when running a get_posts()? One of my queries is performing poorly. To optimize I just need to be able to reuse the same joined table ...
Scruffy Paws's user avatar
9 votes
1 answer
2k views

Mixing regular and custom post types (with meta_query) on home page

Not sure how to achieve this. I'm trying to mix standard posts and custom posts on a site homepage but I only want to display the custom posts if a meta value is set. Displaying the posts works fine '...
lrm's user avatar
  • 103
8 votes
2 answers
4k views

how to show posts that are missing a meta_value

How can I build a query to find posts that DO NOT contain a certain meta key or meta value? for example: query_posts( array( 'meta_query' => array( array( 'key' => 'feature', '...
dwenaus's user avatar
  • 1,242
8 votes
1 answer
12k views

How to query_posts using meta_query to orderby meta_key AND have a secondary sort by date?

I've been troubleshooting an issue over the past few days and keep going in circles. Could really use a fresh pair of eyes to help me answer this question... So I'm working with a wordpress site ...
Dana's user avatar
  • 183
8 votes
3 answers
5k views

Complex meta query with 3 keys

I think the problem essentially relates to sql query structure and I am not an expert.... I need to search for posts (custom post type) by 2 parameters: pd_city pd_country Please note that ...
Dameer's user avatar
  • 197
8 votes
2 answers
1k views

meta_query sorting by 2 keys

I need to sort (custom) posts by 2 custom field values... custom field name 1: is_sponsored [ value can either be 1 or 0 ] custom field name 2: sfp_date [ timestamp aka current post date in seconds ]...
Dameer's user avatar
  • 197
7 votes
1 answer
17k views

Get result from meta_query() between two numbers

I have value like :- if ( ! empty( $_GET['filter-area'] ) ) { $f = $_GET['filter-area']; preg_match_all('!\d+!', $f, $matches); foreach($matches as $key) { $first_val = $key[0]; ...
Almeeraj Tourism's user avatar
6 votes
4 answers
15k views

SELECT max(meta_value) FROM wp_postmeta WHERE meta_key='price'... stops working when value is over 999

Overview: I am not trying to return a post. I simply want the single highest value for a particular meta_value across all posts... just the value itself. Details: I have added a custom meta_key "...
Kirkland's user avatar
  • 298
6 votes
2 answers
24k views

WP_Query() show posts that end later than today

I have a query as follows: $wp_query = new WP_Query( 'meta_key' => 'end_date', 'meta_value' => 'today', 'meta_compare' => '>=', 'post_type' => 'vehicle' ); I ...
Sweepster's user avatar
  • 255
6 votes
1 answer
23k views

How to create a wp_query that contains both meta_query and tax_query

I have a wp_query that works great but I can't have both a meta query and a tax_query in it. <?php $event_query = new WP_Query( array( 'post_type' => 'event', // ...
TJ Sherrill's user avatar
6 votes
3 answers
33k views

Display products from specific category in shop page

I know many people asked for this question but I didn't find a proper way to do it. How to add a simple meta_query (product_cat) before the execution of the shop page's query. Maybe by using a filter ...
miko7358's user avatar
  • 406
5 votes
1 answer
8k views

ACF Relationship Field Search Filtering [closed]

I am trying to assign relate posts using the ACF relationship field. It has typeahead search to search by post titles. I would like to add meta_data search to the ACF relationship search. This ...
Bryan's user avatar
  • 281
5 votes
1 answer
3k views

How to Compare Two Meta Fields

Consider two meta fields balance and monthly_price. How you would get all posts that has balance < monthly_price Thinking of creating meta query but our both fields are arbitrary or do not have ...
Sisir's user avatar
  • 7,772
4 votes
2 answers
3k views

Archives for custom post type based on a "date" meta value

I've got an "Event" custom post type. Each post has a meta field for the actual event date, which cannot be the posts's publish date, as most all the event dates will be in the future. With that, I'm ...
Ryan Palmer's user avatar
3 votes
1 answer
4k views

WordPress query through Products variation stock [closed]

I have WooCommerce variable products and a filter which is filtering products based on variation attributes. I have variation attribute depth which has numeric value (from 1 to 20) and the filter is ...
wplearner's user avatar
  • 489
3 votes
1 answer
11k views

WP_User_Query with combined meta query - not working?

I'm trying to use WP_User_Query class to list all users basing on their specific meta fields. When I use simple single-meta query, it works fine: $q = new WP_User_Query( array( 'role' => ...
Gacek's user avatar
  • 187
3 votes
1 answer
7k views

meta_query with array as value

I am writing some PHP which allows users of my site to submit a form and query posts by their post_meta. Everything works fine except for one thing, I used Advanced Custom Fields to create the ...
tyler's user avatar
  • 113
3 votes
1 answer
14k views

Order Posts by Taxonomy and Meta Value

It's possible to order posts by Meta Value. It's possible to order posts by more than one value. And thanks to the code below it's possible to order by taxonomy. But how can I alter the code below ...
Chris's user avatar
  • 153
3 votes
2 answers
541 views

What is an efficient way to query based on post_meta?

The problem I am having is when I allow users to filter posts it overpowers the database and things run extremely slow. I am unsure what the best way to query posts using meta values is. My site has a ...
tyler's user avatar
  • 113