Comments (10)
Hi @der-lukas,
add the code below in your functions.php
.
Register the queries vars:
add_filter( 'rest_query_vars', function( $valid_vars ) {
return array_merge( $valid_vars, array( 'meta_query', 'meta_key', 'meta_value' ) );
} );
Adding parameters to filter:
add_filter( 'rest_post_query', function( $args, $request ) {
$key = $request->get_param( 'meta_key' );
$value = $request->get_param( 'meta_value' );
if ( 'land' == $key && ! empty( $value ) ) {
$args['meta_query'] = array(
array(
'key' => $key,
'value' => $value,
'compare' => '=',
)
);
}
return $args;
}, 10, 2 );
Cheers
from acf-to-wp-rest-api.
@der-lukas if you want a most friendly url, like: /wp-json/wp/v2/calendar/?land=de-AT
. You can do this:
add_filter( 'rest_query_vars', function ( $valid_vars ) {
return array_merge( $valid_vars, array( 'land', 'meta_query' ) );
} );
add_filter( 'rest_post_query', function( $args, $request ) {
$land = $request->get_param( 'land' );
if ( ! empty( $land ) ) {
$args['meta_query'] = array(
array(
'key' => 'land',
'value' => $land,
'compare' => '=',
)
);
}
return $args;
}, 10, 2 );
from acf-to-wp-rest-api.
Hello,
But...I'm using the last version (3.1.0) already?
Oh, sorry I'm talking about this thread: airesvsg/acf-to-rest-api#123
I'll comment in the right place, sorry :)
from acf-to-wp-rest-api.
@airesvsg
I'm new to wordpress and using "ACF to REST" plugin and have a post_type in relation. Here is the schema:
[
{
"id": 1848,
"date": "2020-04-20T18:08:45",
"date_gmt": "2020-04-20T18:08:45",
"guid": {
"rendered": "http://localhost/wordpress/?post_type=state&p=1848"
},
"modified": "2020-04-20T18:08:45",
"modified_gmt": "2020-04-20T18:08:45",
"slug": "karnataka",
"status": "publish",
"type": "state",
"link": "http://localhost/wordpress/state/karnataka/",
"title": {
"rendered": "Karnataka"
},
"content": {
"rendered": "",
"protected": false
},
"featured_media": 0,
"template": "",
"acf": {
"state": "karnataka",
"country": [
{
"ID": 1832,
"post_author": "1",
"post_date": "2020-04-17 11:02:42",
"post_date_gmt": "2020-04-17 11:02:42",
"post_content": "",
"post_title": "India",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "closed",
"ping_status": "closed",
"post_password": "",
"post_name": "india",
"to_ping": "",
"pinged": "",
"post_modified": "2020-04-17 11:02:43",
"post_modified_gmt": "2020-04-17 11:02:43",
"post_content_filtered": "",
"post_parent": 0,
"guid": "http://localhost/wordpress/?post_type=countries&p=1832",
"menu_order": 0,
"post_type": "countries",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw"
}
]
},
"_links": {
"self": [
{
"href": "http://localhost/wordpress/wp-json/wp/v2/state/1848"
}
],
"collection": [
{
"href": "http://localhost/wordpress/wp-json/wp/v2/state"
}
],
"about": [
{
"href": "http://localhost/wordpress/wp-json/wp/v2/types/state"
}
],
"wp:attachment": [
{
"href": "http://localhost/wordpress/wp-json/wp/v2/media?parent=1848"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": true
}
]
}
}
]
Though, I added the filter in functions.php to filter by ACF field "country", doesn't work for me.
add_filter( 'rest_query_vars', function( $valid_vars ) {
return array_merge( $valid_vars, array( 'meta_query', 'meta_key', 'meta_value' ) );
} );
add_filter( 'rest_state_query', function( $args, $request ) {
$key = $request->get_param( 'meta_key' );
$value = $request->get_param( 'meta_value' );
if ( 'country' == $key && ! empty( $value ) ) {
$args['meta_query'] = array(
array(
'key' => $key,
'value' => $value,
'compare' => '=',
)
);
}
return $args;
}, 10, 2 );
==================================
Query: wp-json/wp/v2/state?meta_key=country&meta_value=India
Can you please help me on this?
from acf-to-wp-rest-api.
Awesome! Thank you so much!
from acf-to-wp-rest-api.
Oh, that's perfect! This way it's also possible to use multiple filters! :) Thanks!
from acf-to-wp-rest-api.
😄 👍
from acf-to-wp-rest-api.
This is not working for multiple filters :(
I'm trying to /wp-json/wp/v2/posts/?city=SP&state=SP
And /wp-json/acf/v3/posts/?city=SP&state=SP
Have tried duplicate the function also pass an array in multiple points of it, nothing have worked. :/
from acf-to-wp-rest-api.
Hello @danilopaulinodasilva,
I'm not give support for this version, please up to date for the most recent version in the link below.
https://github.com/airesveg/acf-to-rest-api
Thanks
from acf-to-wp-rest-api.
@airesvsg
I'm new to wordpress and using "ACF to REST" plugin and have a post_type in relation. Here is the schema:[ { "id": 1848, "date": "2020-04-20T18:08:45", "date_gmt": "2020-04-20T18:08:45", "guid": { "rendered": "http://localhost/wordpress/?post_type=state&p=1848" }, "modified": "2020-04-20T18:08:45", "modified_gmt": "2020-04-20T18:08:45", "slug": "karnataka", "status": "publish", "type": "state", "link": "http://localhost/wordpress/state/karnataka/", "title": { "rendered": "Karnataka" }, "content": { "rendered": "", "protected": false }, "featured_media": 0, "template": "", "acf": { "state": "karnataka", "country": [ { "ID": 1832, "post_author": "1", "post_date": "2020-04-17 11:02:42", "post_date_gmt": "2020-04-17 11:02:42", "post_content": "", "post_title": "India", "post_excerpt": "", "post_status": "publish", "comment_status": "closed", "ping_status": "closed", "post_password": "", "post_name": "india", "to_ping": "", "pinged": "", "post_modified": "2020-04-17 11:02:43", "post_modified_gmt": "2020-04-17 11:02:43", "post_content_filtered": "", "post_parent": 0, "guid": "http://localhost/wordpress/?post_type=countries&p=1832", "menu_order": 0, "post_type": "countries", "post_mime_type": "", "comment_count": "0", "filter": "raw" } ] }, "_links": { "self": [ { "href": "http://localhost/wordpress/wp-json/wp/v2/state/1848" } ], "collection": [ { "href": "http://localhost/wordpress/wp-json/wp/v2/state" } ], "about": [ { "href": "http://localhost/wordpress/wp-json/wp/v2/types/state" } ], "wp:attachment": [ { "href": "http://localhost/wordpress/wp-json/wp/v2/media?parent=1848" } ], "curies": [ { "name": "wp", "href": "https://api.w.org/{rel}", "templated": true } ] } } ]
Though, I added the filter in functions.php to filter by ACF field "country", doesn't work for me.
add_filter( 'rest_query_vars', function( $valid_vars ) { return array_merge( $valid_vars, array( 'meta_query', 'meta_key', 'meta_value' ) ); } ); add_filter( 'rest_state_query', function( $args, $request ) { $key = $request->get_param( 'meta_key' ); $value = $request->get_param( 'meta_value' ); if ( 'country' == $key && ! empty( $value ) ) { $args['meta_query'] = array( array( 'key' => $key, 'value' => $value, 'compare' => '=', ) ); } return $args; }, 10, 2 );
==================================
Query: wp-json/wp/v2/state?meta_key=country&meta_value=India
Can you please help me on this?
same issue for me
from acf-to-wp-rest-api.
Related Issues (16)
- Questions about Attachment acf data in Gallery Field HOT 5
- Rename from 'acf' in response HOT 2
- Orderby ACF field HOT 12
- Field Not Updating
- Retrieving ACFs from custom Option Pages HOT 2
- v3 orderby issue
- Getting post/object by field HOT 1
- Full Guide on Posting and Getting ACF data HOT 1
- Will this support post? HOT 10
- Shortcodes HOT 3
- Getting ACF data for a set of posts? HOT 1
- ACF not showing in Custom Post type HOT 13
- All request endpoints return null HOT 3
- Request to page HOT 4
- Including Custom Post Types & Corresponding ACF Data HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from acf-to-wp-rest-api.