Custom Fields
Re-Envisioned
Support General Query REST API by Meta Box Value

This topic contains 1 reply, has 2 voices, and was last updated by  Anh Tran 2 weeks, 1 day ago.

  • Creator
    Topic
  • #15201

    joeporterme
    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

Viewing 1 replies (of 1 total)
  • Author
    Replies
  • #15220

    Anh 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 1 replies (of 1 total)

You must be logged in to reply to this topic.