• Plugin Author Phi Phan

    (@mr2p)


    My recommendation for those complex use cases is to register a rest field. Here is the demo code:

    // Create a function to build rest field value.
    function yourprefix_get_rest_field( $post_id ) {
    	return 'calculate field value from $post_id';
    }
    
    // Register a rest field.
    add_action( 'rest_api_init', function () {
      register_rest_field(
        'post',
        'your_rest_field_name',
        array(
          'get_callback' => function ( $post_array ) {
            return yourprefix_get_rest_field( $post_array['id'] );
          },
          'schema'       => array(
            'type' => 'string',
          ),
        )
      );
    });
    
    // Display the content of the rest field.
    add_filter( 'meta_field_block_get_block_content', function ( $content, $attributes, $block, $post_id ) {
      $field_name = $attributes['fieldName'] ?? '';
    
      if ( 'your_rest_field_name' === $field_name ) {
        return yourprefix_get_rest_field( $post_id );
      }
    
      return $content;
    }, 10, 4);
  • The topic ‘How to display a complex value that depends on multiple meta fields?’ is closed to new replies.