0

I have added a custom post type and I want to expose the custom field value in the rest api. As per the docs register_meta can be used.

I have created a custom post meta box using the wordpress way. It works fine but not getting shown in the rest api. I belive that I have to pass array

enter image description here

add_action( 'rest_api_init', 'qrcode_register_posts_meta_field' );

function qrcode_register_posts_meta_field(){
  $meta_args = array( // Validate and sanitize the meta value.
    'type'         => 'array',
    
    'description'  => 'A meta key associated with a string meta value.',
   
    'single'       => true,
   
    'show_in_rest' => array(
      'schema'=> array(
        'type'=> 'array',
        'items'=> array(
          'type'=> 'string',
        ),
      ),
    ),
);
  register_meta( 'post', 'qrcode_qr-code-type', $meta_args );
}

2 Answers 2

0

Found: I forgot to enable the Custom Fields for the custom post type even if the meta boxes were programmatically created.

2
  • And how did you do that?
    – markmoxx
    Commented Mar 17, 2021 at 13:13
  • @markmoxx just noticed this. I was using custom post ui so I had an option there. If you coded it you might have the use the supports
    – user145078
    Commented Mar 30, 2021 at 22:59
0

For those programmatically creating the CPT, be sure to also support 'custom-fields' in your post type, as noted in https://developer.wordpress.org/reference/functions/register_post_meta/#user-contributed-notes

register_post_type( 'book', array(
    'supports' => array( 'title', 'editor', 'custom-fields', ...),
     // Make sure you add custom-fields here ^^^^^^^^^^^^^
    )
);
2
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – cjbj
    Commented Dec 17, 2022 at 21:55
  • Answer is repeat of previous answer
    – Lovor
    Commented Dec 22, 2022 at 9:09

Your Answer

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