Support

Account

Home Forums Feature Requests Read-Only Field Reply To: Read-Only Field

  • @pkhunter I wanted to created a read only “relationship” field that gets updated bidirectionally from a linked post. This is the solution I am using:

    // render readonly field on relationship settings
    add_action('acf/render_field_settings/type=relationship', function($field) {
      acf_render_field_setting( $field, array(
        'label'     => __('Read Only?','acf'),
        'instructions'  => '',
        'type'      => 'true_false',
        'name'      => 'readonly',
      'ui'		=> 1,
      'class'	=> 'acf-field-object-true-false-ui'
      ));  
    });
    
    // render relationships as links to the edit pages.
    add_filter('acf/prepare_field/type=relationship', function($field) {
      // check to see if this is a read only relationship field
      if (isset($field['readonly']) && $field['readonly']) {
        // the value is an array of post_ids
        $post_ids = $field['value'];
        // render them as a list
        echo "<ul style='margin-left:5px;'>";
        foreach($post_ids as $post_id) {
          $post = get_post($post_id);
          $edit_link = get_edit_post_link($post_id);
          echo "<li><a href='$edit_link'>" . $post->post_title . "</a></li>";
        }
        echo "</ul>";
        // return false to stop default rendering
        return false;
      }
      return $field;
    });