Filter by field with array value in ACF on WP REST API

Advanced Custom Fields stores values for multiple select fields as a serialised Array. For a select field with options ‘one’ and ‘three’ selected, the value stored on the database would look like this: ‘a:2:{i:0;s:3:”one”;i:1;s:5:”three”;}’ It is simply not possible with WP_Query (which is where your meta query ends up) or SQL to filter results based … Read more

How to add an endpoint for my custom post type? /wp-json/wp/v2/posts is working but it didn’t in the custom post

The “register_rest_field” function will add and handle data update for a given endpoint, looks like it’s not what you are looking for. The register_post_type() function accepts a “show_in_rest” and “rest_base” params if you want to use a custom slug for the endpoint: https://codex.wordpress.org/Function_Reference/register_post_type By default it uses the WP_REST_Posts_Controller class to handle the methods (get, … Read more

Send a get request to wordpress

One option would be to create a Page dedicated to processing these GET requests. Something like http://example.com/create/?yourquerystringhere`. You can then create a page template, either page-create.php to automatically apply to that page, or a custom template you manually apply from the dropdown. That page template can contain your custom code: <?php // if any variables … Read more

Custom wp-query in wordpress rest api

You can create a route for Rest API using the below code. Simply put this code in your function.php file /* Route For api */ add_action( ‘rest_api_init’, function () { register_rest_route(‘wp/v2’, ‘test’, array( ‘methods’ => array(‘GET’, ‘POST’), ‘callback’ => ‘test’, )); } ); And then simply create a callback function test and put your stuff … Read more

400 Bad Request – JavaScript App calling Custom wp-json endpoint

There were three problems I discovered with my code above. First, $_SERVER[“HTTP_REFERRER”]; is an undefined index, moreover it is mispelled – HTTP_REFERER would be correct if it could be accessed this way in WP. I found get_http_origin() instead. $data[‘UrlReferer’] = get_http_origin(); Second, I also mispelled the field I was saving this variable to – UrlReferrer … Read more

Extending REST API responses

You can modify the response data via the rest_prepare_{$this->post_type} filter, like so for a custom post type test_cpt (registered with hierarchical and show_in_rest set to true): add_filter( ‘rest_prepare_test_cpt’, ‘rest_prepare_test_cpt’ ); function rest_prepare_test_cpt( WP_REST_Response $response ) { $data = $response->get_data(); if ( ! empty( $data[‘parent’] ) ) { if ( ! $parent_post = get_post( $data[‘parent’] ) … Read more