简体   繁体   中英

Retrieve posts by custom field with WP-REST-API in Javascript

What is the right URL to retrieve posts by meta_query with the WP-REST-API?

The custom field I want to use can contain multiple values, I tried this for custom field which only can contain a single value and this works.

wp-json/posts?format=json&filter[meta_key]=content_type&filter[meta_value]=2

But I can't get it work with a custom field which can contain multiple values (array/object). Anyone?

You need the ACF to REST API plugin. In addition to that, you need to make a few customizations such as:

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

    return $args;
} );

See https://github.com/airesvsg/acf-to-rest-api/issues/122#issuecomment-291913932

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM