Skip to content
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

Filter by ACF Field Value #6

Closed
der-lukas opened this issue Dec 19, 2015 · 10 comments
Closed

Filter by ACF Field Value #6

der-lukas opened this issue Dec 19, 2015 · 10 comments

Comments

@der-lukas
Copy link

Hey! Thanks for this great plugin!

I already had a look at the other issues here, but still can't get it to work...

I want to get all the posts from the CPT "calendar" with a specific ACF-Field Value.

Would it be possible to request it like this:
".../wp-json/wp/v2/calendar/?meta_key=land&meta_value=de-AT"

Thanks in advance!

Lukas

@airesvsg
Copy link
Owner

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

@der-lukas
Copy link
Author

Awesome! Thank you so much!

@airesvsg
Copy link
Owner

@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 );

@der-lukas
Copy link
Author

Oh, that's perfect! This way it's also possible to use multiple filters! :) Thanks!

@airesvsg
Copy link
Owner

😄 👍

@danilopaulinodasilva
Copy link

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

@airesvsg
Copy link
Owner

airesvsg commented Jan 22, 2018

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

@danilopaulinodasilva
Copy link

danilopaulinodasilva commented Jan 22, 2018

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 :)

@mkchetan25
Copy link

mkchetan25 commented Apr 21, 2020

@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?

@Shafran123
Copy link

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants