Query REST API by Meta Box Value

Support General Query REST API by Meta Box Value

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #15201
    GreatNews.LifeGreatNews.Life
    Participant

    is there a built in way to do this? Ive googled and googled before posting... I have metabox taxonomy select fields for posts... and id like the API to show posts by metabox/taxonomy

    #15220
    Anh TranAnh Tran
    Keymaster

    Hi,

    Did you mean querying posts by custom fields?

    If so, please try this code:

    add_filter( 'rest_{type}_query', function( $args ) {
        $args['meta_query'] = array(
            array(
                'key'   => 'my_field',
                'value' => esc_sql( $_GET['field'] ),
            )
        );
    
        return $args;
    } );

    REST URL:

    http://site.com/wp-json/wp/v2/posts?field=test
    

    The wildcard {type} can be: post | user | comment | attachment | custom post type | taxonomy.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.