-
-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Filtering by ACF field #123
Comments
I second this. What's the request structure needed to search by ACF field values? For example, I have a Location custom post type. One of my ACF fields is 'City.' What if I wanted to return all Location posts whose City value is 'New York' ? I'd imagine this looking something like But that didn't seem to work. |
Hi, For both cases, use the filter below. add_filter( 'rest_{type}_query', function( $args ) {
$args['meta_query'] = array(
array(
'key' => 'tag',
'value' => esc_sql( $_GET['tag'] ),
)
);
return $args;
} ); @TheBalco replace {type} to attachment Thanks |
Is this solution work if I need this filter URL?
|
Hi @amindiary, |
Hi @airesvsg , I'm sorry for what may be a stupid question, but here it goes. I'm not sure how to expand the above example for my use case. Currently the data structure I am getting back from the api, looks like this
}, I would like to be able to filter by the value of acf.State.post_title. for example something like: /childcare/wp-json/acf/v3/substandarddata/?filter[State]=Arizona?filterper_page=-1 Is this possible? |
How can I make it work with multiple filters? Like @jchiatt In my case I have multiple fields like city, state, region code (array). I need to filter multiple fields. I'm trying to /wp-json/wp/v2/posts/?city=SP&state=SP Have tried duplicate the function also pass an array in multiple points of it, nothing have worked. :/ |
add_filter( 'rest_{type}_query', function( $args ) {
$args = array();
$fields = array( 'city', 'state', 'region', 'code' );
foreach ( $fields as $field ) {
if ( isset( $_GET[ $field ] ) && ! empty( $_GET[ $field ] ) ) {
$args['meta_query'][] = array(
'key' => $field,
'value' => esc_sql( $_GET[ $field ] ),
);
}
}
return $args;
} ); |
Não deu certo! hehe vou abrir um novo tópico, acho que estou quase na solução. Mais fácil, concorda? It's not works! I'll open a new issue, I think a have a solution. It's ease, whats you think? |
I found the solution, it was actually that @airesvsg just forgot a bit of code in his latest code excerpt just above. In the $args[] = array( should be $args['meta_query'][] = array( The rest is fine. All the necessary info about how to filter using custom fields for a custom post type API query is right here in the WordPress Codex: WP Meta Query class. |
Hey all, I was having some issues with the snippets in this thread. This ended up working the best for me: add_filter( 'rest_{type}_query', function( $args ) {
$ignore = array('per_page', 'search', 'order', 'orderby', 'slug');
foreach ( $_GET as $key => $value ) {
if (!in_array($key, $ignore)) {
$args['meta_query'][] = array(
'key' => $key,
'value' => $value,
);
}
}
return $args;
}); The |
Hi all, I can't get any of this to work. I have a date ACF and I need to access it via the rest API url. I have added @airesvsg code and changed $fields array with my field Then I've tried @bretmorris solution but when I go to What am I doing wrong? |
Hi, I added the code that you mentioned for specific query but now I can't get the list from the acf rest and also the wp rest. Like /wp-json/wp/v2/books (not work anymore) This /wp-json/wp/v2/books?title=abc (this work) Can you please show me how to make both works? |
For anyone looking for a step by step guide, here's how to do it:
add_filter( 'rest_user_query', function( $args ) {
$fields = array( 'field1', 'field2', 'field3', 'field4' );
foreach ( $fields as $field ) {
if ( isset( $_GET[ $field ] ) && ! empty( $_GET[ $field ] ) ) {
$args['meta_query'][] = array(
'key' => $field,
'value' => esc_sql( $_GET[ $field ] ),
);
}
}
return $args;
} ); now you can filter users with acf fields like this: Hope this helps. |
Hi @llino, Never edit your wp-includes/functions.php to paste a code. You should paste the snippet of code in your functions.php of your current theme. Thanks |
Ak, ok. |
that bringsme a [ ] please you can help me? |
I have this data structure: {
"id": 3910,
"acf": {
"weekDay": "",
"title": "Bebida Grátis na Compra de um Burger",
"description": "Na compra de qualquer burger ganhe uma bebida. Na compra de qualquer burger
ganhe uma bebida. Na compra de qualquer burger ganhe uma bebida.",
"priceBefore": "",
"priceAfter": "",
"image": {},
"image_1": false,
"image_2": false,
"image_3": false,
"": false,
"active_coupon": true,
"coupon_code": "AAA"
}
} I applied this code to functions.php inside my theme:
I want to filter by active_coupon == true, so I try: http://site.com.br/wp-json/acf/v3/posts?active_coupon=true But now it's returning an empty array: [] Can anyone help me? |
replace {type} with user, post, location, something else. add_filter( 'rest_{type}_query', function( $args ) {
$fields = array( 'city', 'state', 'region', 'code' );
foreach ( $fields as $field ) {
if ( isset( $_GET[ $field ] ) && ! empty( $_GET[ $field ] ) ) {
$args['meta_query'][] = array(
'key' => $field,
'value' => esc_sql( $_GET[ $field ] ),
);
}
}
return $args;
} ); |
@llino I have the same problem as @juanfran1512 and I already replaced {type} with post. Can you help me? |
@Sthefanny add_filter( 'rest_post_query', function( $args ) {
$fields = array( 'city', 'state', 'region', 'code' );
foreach ( $fields as $field ) {
if ( isset( $_GET[ $field ] ) && ! empty( $_GET[ $field ] ) ) {
$args['meta_query'][] = array(
'key' => $field,
'value' => esc_sql( $_GET[ $field ] ),
);
}
}
return $args;
} ); |
@llino Didn't work |
Hi @sthefany, It's worked for you?? Let's me know. Thanks |
@airesvsg No it didn't work. But I'm now using it with "WP REST Filter" plugin and I can do what I need |
Hi @airesvsg, In the project I'm working, I need to return if there're any value set in the field. Something like this: /wp-json/wp/v2/posts?field=true So doesn't matter what value is, since has something in the field it needs to return data from the search. Can u help me? |
I've already found the solution :D |
Hi @airesvsg , I need to filter by ACF field, which is an array. For a single value this code works:
But how can I filter all custom posts, when |
I'm also running into problems with this... Tried every piece of code on this thread, and none worked all the way. This one below sort of worked, but with issues:
This helped to filter by some ACF fields, but now I'm not able to access api pages like Any way to refactor this to still be able to use default wordpress API filters/search? |
Ahh, my misunderstanding. I realized I should add anything like that to the $ignore array. I added 'page', and it now works. |
@GabeRivera were you able to figure out the answer to your question? I'm also trying to figure out how to query nested fields, and all the answers here don't seem to be for that. |
Tried all the solutions here, like others, I got an empty |
Hello, I would like to filter all ACF data, how should I do this? |
Hello,
I'm using ACF to "categorize" my media items. With this plugin (v2.2.1 over wp-plugin-store) I'm able to see the custom field (acf->tag) in the wp-json-response.
How can I filter these values?
I'd like to to send a GET-Request just like this to get only specific media:
http://wordpress.dev/wp-json/wp/v2/media?tag=my-image-tag
I saw some examples with putting some code into
functions.php
, but I don't know which file I should change - it didn't work when I tried it.On the other side I want to create media with this specific tag. So when I create media over the API, I want to set the ACF field value. How can I achieve this?
Sorry for this, maybe I'm just too stupid for this, but it's my first project with wordpress ^^
The text was updated successfully, but these errors were encountered: