1

I am getting posts with a certain term by using /wp-json/wp/v2/my_post_type?my_categorie=43 but upon inspecting the JSON object I noticed that the meta is empty? I am using Advanced Custom Fields and I would like to get all the custom fields of that post type in the meta[].

I did manage to do it in the old legacy way but the meta is missing some info about the advanced custom field. For example in my advanced custom field, I use a prepend for a price field which puts a euro sign in front of the price. I noticed that in the old legacy way the prepend is missing.

Then I looked at some functions from Advanced Custom Fields https://www.advancedcustomfields.com/resources/ and found that they have get_field_objects() to get the fields from a post by using it's id. This did give me everything I needed, but only for one post. The problem is, this can only be done for one post at the time as far as I could see, so I think this would be really slow because I would need to get all the posts that match the term and then execute this function for every post which would make a database call to get the fields. I would like to do this for all the posts in my custom post type that match a term.

But the strange thing is when using get_field_objects() and checking the network tab, I couldn't really find a get that coresponds to this? So I'm not really sure if it even makes a database call. It must be right? How else would it get those fields? Also, If I would want to call get_field_objects() I would have to do it in the old legacy way.

How I currently get them. This is pretty slow

function do_get_posts_by_term_with_fields()
{
    if (!isset($_GET) || !isset($_GET['post_type']) || !isset($_GET['taxonomy_name']) || !isset($_GET['term_id'])) {
        echo "There is something wrong with your get request.";
        die;
    }

    $post_type = $_GET['post_type'];
    $term_id = $_GET['term_id'];
    $taxonomy_name = $_GET['taxonomy_name'];

    $args = array(
        'post_type'   => $post_type,
        'tax_query' => array(
            array(
                'taxonomy' => $taxonomy_name,
                'field' => 'id',
                'terms' => $term_id,
                'include_children' => false
            )
        )
    );
    $posts = get_posts($args);
    $posts_with_meta =  array_map('do_add_fields_to_posts', $posts);
    echo (json_encode($posts_with_meta));
    die;
}
add_action('wp_ajax_get_posts_by_term_with_fields', 'do_get_posts_by_term_with_fields');
add_action('wp_ajax_nopriv_get_posts_by_term_with_fields', 'do_get_posts_by_term_with_fields');

function do_add_fields_to_posts($post)
{
    $post->meta = get_field_objects($post->ID);
    return $post;
}

1 Answer 1

1

Firstly, your question mentions the REST API, but you don't actually appear to be using the REST API. You're using admin-ajax.php. It's an important difference in the context of your question.

Secondly, get_field_objects() function is an ACF function that gets data about the ACF fields. Not the meta, the literal fields created by ACF. So you'll get things like the field type, label, instructions etc. Not just the value.

If you want all post meta assigned to a post, then you should use get_post_meta() without specifying a key:

$post->meta = get_post_meta( $post->ID );

All that being said, for your use case you should really just use the REST API. You'll just need to use register_post_meta() to add your desired meta keys to the response, as documented here:

$meta_args = array(
    'type'         => 'string',
    'description'  => 'A meta key associated with a string meta value.',
    'single'       => true,
    'show_in_rest' => true,
);
register_post_meta( 'page', 'my_meta_key', $meta_args );

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.