Raphael Koszalka
Raphael Koszalka

Reputation: 3

Adding ACF to CUSTOM wp api endpoints

I'm writing a custom endpoint for WP API, to retrieve posts from wordpress that for example are from the 'real_estate' post_type and capacity for '5 or more' persons.

I've built a new custom endpoint:

// permite que meta_key e meta_value 
// sejam filtrados pela api
function filtros( $valid_vars ) {
    $valid_vars = array_merge( 
        $valid_vars, 
            array( 
                'meta_key', 
                'meta_value' ) );
    return $valid_vars;
}
add_filter( 'rest_query_vars', 'filtros' );
// funcção que retorna posts do autor
function busca( $data ) {
    $posts = get_posts(array(
        'post_type' => 'imoveis',
        'posts_per_page'    =>  '1000',
        'meta_query'    => array(
            'relation'  =>  'AND',
            array(
                'key'   =>  'transacao',
                'value' => $data['tipo']
            ),
            array(
                'key'   =>  'quartos',
                'value' => $data['quartos'],
                'compare'   =>  '>'
            )
        )
    ));

    if ( empty( $posts ) ) {
        return new WP_Error( 'sem resultados', 'quartos: ' . $data['quartos'] . ' transacao: '. $data['tipo'], array( 'status' => 404 ) );
    }
    return $posts;
}
// cria o endpoint que ira receber a função acima
add_action( 'rest_api_init', function () {
    register_rest_route( 'busca/v2', '/resultado/(?P<tipo>.+)/(?P<quartos>\d+)', 
        array(
            'methods' => 'GET',
            'callback' => 'busca',

        ) 
    );
});

The search is fine, it's working, i'm filtering by transaction type (sale or rent) and number of rooms in each real estate.

But my JSON response is missing a lot of fields, including ACF. EX: {
"ID":149, "post_author":"2", "post_date":"2016-03-03 23:53:39", "post_date_gmt":"2016-03-03 23:53:39", "post_content":"", "post_title":"Oportunidade do Ano", "post_excerpt":"", "post_status":"publish", "comment_status":"closed", "ping_status":"closed", "post_password":"", "post_name":"oportunidade-do-ano", "to_ping":"", "pinged":"", "post_modified":"2016-03-03 23:53:39", "post_modified_gmt":"2016-03-03 23:53:39", "post_content_filtered":"", "post_parent":0, "guid":"http://raphaelk.co/api/?post_type=imoveis&p=149", "menu_order":0, "post_type":"imoveis", "post_mime_type":"", "comment_count":"0", "filter":"raw" },

Do you guys have any idea how can i change that response? And include ACF to it.

Thank you

Upvotes: 0

Views: 2189

Answers (1)

qbeauperin
qbeauperin

Reputation: 589

Did you try to simply use the ACF function get_fields ?

In your "busca" function, after get_posts(), if $posts isn't empty, retrieve acf fields for each posts like this :

if ( empty( $posts ) ) {
  return new WP_Error( 'sem resultados', 'quartos: ' . $data['quartos'] . ' transacao: '. $data['tipo'], array( 'status' => 404 ) );
} else {
    foreach ($posts as $key => $post) {
        $posts[$key]->acf = get_fields($post->ID);
    }
}

Hopefully that'll do it !

Upvotes: 2

Related Questions