• Resolved stehoss

    (@stehoss)


    Iam using the “ACF” plugin with “ACF to REST API”. I found a solution for querying a custom field over REST API.

    I inserted this into my functions.php:

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

    Url: /wp-json/wp/v2/posts?field=test The wildcard {type} can be: post | user | comment | attachment | custom post type | taxonomy

    This solution is not perfect. Once this code snipped is added, the filter works but if you go to /v2/{type} <– which usually lists all, this stops working.

    Any solution for that?

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Thread Starter stehoss

    (@stehoss)

    I got it ??

    add_filter('rest_test_query', function($args) {
    	
    	$args['meta_query'] = array(
    	array(
    		'key'   => 'test_1',
    		'value' => esc_sql( $_GET['test_1'] ),
    		)
    	);
    	
    	//echo $args['meta_query'][0]['value'];
    	//print_r($args);
    	
    	if (!empty($args['meta_query'][0]['value'])) {
    		return $args;		
    	}
    	
    } );
    • This reply was modified 2 years, 4 months ago by stehoss.
Viewing 1 replies (of 1 total)
  • The topic ‘querying to custom field over ACF REST API’ is closed to new replies.