Git Product home page Git Product logo

Comments (10)

airesvsg avatar airesvsg commented on July 28, 2024 5

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.

airesvsg avatar airesvsg commented on July 28, 2024 1

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

danilopaulinodasilva avatar danilopaulinodasilva commented on July 28, 2024 1

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.

mkchetan25 avatar mkchetan25 commented on July 28, 2024 1

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

der-lukas avatar der-lukas commented on July 28, 2024

Awesome! Thank you so much!

from acf-to-wp-rest-api.

der-lukas avatar der-lukas commented on July 28, 2024

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

from acf-to-wp-rest-api.

airesvsg avatar airesvsg commented on July 28, 2024

😄 👍

from acf-to-wp-rest-api.

danilopaulinodasilva avatar danilopaulinodasilva commented on July 28, 2024

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.

airesvsg avatar airesvsg commented on July 28, 2024

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.

Shafran123 avatar Shafran123 commented on July 28, 2024

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.