0

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

1 Answer 1

2

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 !

Sign up to request clarification or add additional context in comments.

2 Comments

@RaphaelKoszalka Happy to help ! Don't forget to accept the answer. ;)
Sorry, i forgot to do that the first time. Done, answer accepted!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.