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 #13

Closed
pakoArtal opened this Issue Feb 26, 2016 · 6 comments

Comments

Projects
None yet
4 participants
@pakoArtal

pakoArtal commented Feb 26, 2016

Hi Aires,

I'm trying to filter by ACF Field Value, just the same question asked for the previous version at airesvsg/acf-to-wp-rest-api#6.

Will your answer still work for this version? I tried your code but I'm not able to filter, getting all the results

I'm using:

  • WordPress 4.4.2
  • WP REST API Versión 2.0-beta12
  • Custom Post Type UI Versión 1.2.3 (Admin panel for creating custom post types and custom taxonomies in WordPress)
  • Advanced Custom Fields Versión 4.4.5
  • ACF to REST API Versión 2.0.6
@airesvsg

This comment has been minimized.

Show comment
Hide comment
@airesvsg

airesvsg Feb 26, 2016

Owner

Hi @pakoArtal,
If your Custom Post is "books", so your filter will be: rest_books_query = rest_{post_type}_query

add_filter( 'rest_query_vars', function ( $valid_vars ) {
    return array_merge( $valid_vars, array( 'land', 'meta_query' ) );
} );
add_filter( 'rest_books_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 );

Cheers

Owner

airesvsg commented Feb 26, 2016

Hi @pakoArtal,
If your Custom Post is "books", so your filter will be: rest_books_query = rest_{post_type}_query

add_filter( 'rest_query_vars', function ( $valid_vars ) {
    return array_merge( $valid_vars, array( 'land', 'meta_query' ) );
} );
add_filter( 'rest_books_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 );

Cheers

@pakoArtal

This comment has been minimized.

Show comment
Hide comment
@pakoArtal

pakoArtal Feb 27, 2016

Hi @airesvsg ,

You were right. I forgot to change my filter to rest_books_query

Thank you for your help and for your plugin!

pakoArtal commented Feb 27, 2016

Hi @airesvsg ,

You were right. I forgot to change my filter to rest_books_query

Thank you for your help and for your plugin!

@Idealien

This comment has been minimized.

Show comment
Hide comment
@Idealien

Idealien Jul 5, 2016

In that case, what would the end point be?
/wp-json/acf/v2/books?land=america ??

Nope - /wp-json/wp/v2/books?land=america

Awesome!

Idealien commented Jul 5, 2016

In that case, what would the end point be?
/wp-json/acf/v2/books?land=america ??

Nope - /wp-json/wp/v2/books?land=america

Awesome!

@airesvsg

This comment has been minimized.

Show comment
Hide comment
@airesvsg

airesvsg Jul 6, 2016

Owner

Hi @Idealien,
the endpoint /wp-json/acf/v2/books it is not supported in current version.
Please, see this issue: #51
Thanks

Owner

airesvsg commented Jul 6, 2016

Hi @Idealien,
the endpoint /wp-json/acf/v2/books it is not supported in current version.
Please, see this issue: #51
Thanks

@Idealien

This comment has been minimized.

Show comment
Hide comment
@Idealien

Idealien Jul 6, 2016

Yeah; I edited trying to articulate that the /wp-json/wp/v2/books?land=america endpoint serves identical functionality for now. Either way, plugin is amazing and opening up so many front-end doors for development as ACF does to the admin side of WP.

Thanks!

Idealien commented Jul 6, 2016

Yeah; I edited trying to articulate that the /wp-json/wp/v2/books?land=america endpoint serves identical functionality for now. Either way, plugin is amazing and opening up so many front-end doors for development as ACF does to the admin side of WP.

Thanks!

@honeyamin

This comment has been minimized.

Show comment
Hide comment
@honeyamin

honeyamin Jun 10, 2018

what about using compare = IN
and also how can we use in loop?
if ( ! empty( $land ) ) { $args['meta_query'] = array( array( 'key' => 'land', 'value' => $land, 'compare' => 'IN', ) ); }

honeyamin commented Jun 10, 2018

what about using compare = IN
and also how can we use in loop?
if ( ! empty( $land ) ) { $args['meta_query'] = array( array( 'key' => 'land', 'value' => $land, 'compare' => 'IN', ) ); }

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