• Resolved Jo Sprague

    (@josiahsprague)


    We’re wanting to view custom fields that we created with Types via the official WP-API https://wp-api.org/

    We’ve written a custom function that can add any post_meta custom fields to the API response, but right now we’re manually listing each field that we want to add to the API response. We’d like to automatically add all custom fields created with Types to the API response. Is there a function we can use in our custom plugin to get a list of the custom fields added via Types?

    https://www.ads-software.com/plugins/types/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Jo Sprague

    (@josiahsprague)

    I was able to apply the following function with the json_prepare_post filter to get all custom fields created by the Types plugin into the WP-API post response under the types_custom_meta key. The Types function I needed was types_get_fields().

    // Add custom fields created by Types plugin to public types_custom_meta key
    function add_types_custom_meta($data, $post, $context) {
    
        $post_custom_data = get_post_custom( $post['ID'] );
    
        // Get a list of custom fields added by Types plugin
        $all_types_fields = types_get_fields();
    
        $post_custom_types_fields = array();
        foreach ( $all_types_fields as $key => $value ) {
           $post_custom_types_fields[] = $value['meta_key'];
        }
    
        foreach ( $post_custom_data as $key => $value ) {
            if ( in_array($key, $post_custom_types_fields) ) {
                $types_custom_meta[$key] = $value;
            }
        }
    
        if ( !empty($types_custom_meta) ) {
            $data['types_custom_meta'] = $types_custom_meta;
        }
    
        return $data;
    }
    add_filter( 'json_prepare_post', 'add_types_custom_meta', 10, 3 );
    Thread Starter Jo Sprague

    (@josiahsprague)

    I tried using types_get_fields_by_group('public') to only get fields within a group named public, but so far I haven’t been able to get that to work.

    Thread Starter Jo Sprague

    (@josiahsprague)

    Here is the function I wrote to use types_get_fields_by_group() instead;

    // Add custom fields created by Types plugin to public types_custom_meta key
    function add_types_custom_meta($data, $post, $context) {
        if (function_exists(types_get_fields_by_group)) {
            $post_custom_data = get_post_custom( $post['ID'] );
    
            // Get a list of Types custom fields in the "public" group
            $public_types_fields = types_get_fields_by_group('public');
    
            foreach ( $post_custom_data as $key => $value ) {
                if ( in_array($key, array_keys($public_types_fields)) ) {
                    $types_custom_meta[$key] = $value;
                }
            }
    
            if ( !empty($types_custom_meta) ) {
                $data['types_custom_meta'] = $types_custom_meta;
            }
        }
    
        return $data;
    }
    add_filter( 'json_prepare_post', 'add_types_custom_meta', 10, 3 );

    This may sound like a dumb question but where exactly is this code added? Is it the theme’s function.php file or one of the files within the wp-api plugin?
    Any help would be greatly appreciated as we have been looking for the same solution to display custom fields associated with a custom post type. In our case, custom fields that are created using the post types and plugins included with the theme we purchased.
    Our goal is to get the custom fields on this page: https://www.myvipclubs.com/test/city/new-york/listing/asian-art-museum-3/ to be displayed in the corresponding json data here: https://www.myvipclubs.com/test/wp-json/posts/128
    If we are blessed with someone who knows, ultimately we would like to display a select amount of these custom fields within a list of several posts within a custom post type: https://www.myvipclubs.com/test/wp-json/city/new-york/post/type/listing/taxonomy/listingcategory/category_id/4 Thank you in advance for any help on this.

    Thread Starter Jo Sprague

    (@josiahsprague)

    Hi brandecho, it should work in either place. They key is the add_filter line. That makes it run whenever the json_prepare_post hook runs. That being said, it’s generally a bad idea to modify another person’s plugin directly, since your changes would be overwritten in the next update. I’d recommend adding this either to your theme’s functions.php file (easiest) or to a custom plugin’s functions.php. Good luck!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Accessing custom field types via WP API’ is closed to new replies.