Skip to content

Commit dc846d3

Browse files
committed
Added format value to return just an array of child IDs
1 parent 68dca39 commit dc846d3

File tree

2 files changed

+118
-114
lines changed

2 files changed

+118
-114
lines changed

acf-child-post-field-v5.php

Lines changed: 111 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,13 @@ function render_field( $field ) {
315315

316316
<?php echo $before_fields; ?>
317317

318+
318319
<?php
319320
// prevent childbuilder field from creating multiple conditional logic items for each row
320321
$sub_field = $field['sub_fields'][0];
321322
$sub_field['conditional_logic'] = 0;
322-
323+
$sub_field['wrapper']['class'] = $sub_field['wrapper']['class'] . ($i === 'acfcloneindex' ? 'acf-child-post-field-hide' : '');
324+
323325
$acf_child_field_post_id = '';
324326
// add value
325327
if ( isset( $row[$sub_field['key']] ) ) {
@@ -333,93 +335,106 @@ function render_field( $field ) {
333335
$sub_field['value'] = $acf_child_field_post_id;
334336
// update prefix to allow for nested values
335337
$sub_field['prefix'] = "{$field['name']}[{$i}]";
336-
337-
// render input
338-
acf_render_field_wrap( $sub_field, $el );
339-
340-
341-
$post = get_post( $acf_child_field_post_id );
342-
343-
if ( $field['include_title_editor'] ) {
344-
acf_render_field_wrap( acf_get_valid_field( array(
345-
'name' => "{$field['name']}[{$i}][post_data][post_title]",
346-
'label' => 'Title',
347-
'type' => 'text',
348-
'value' => $post->post_title,
349-
'required' => true
350-
) ), $el );
338+
if ($acf_child_field_post_id){
339+
$post = get_post( $acf_child_field_post_id );
340+
} else {
341+
$post = new stdClass();
342+
$post->post_title = '';
343+
$post->post_content = '';
344+
$post->post_excerpt = '';
345+
$post->ID = '';
351346
}
347+
348+
?>
349+
<div class="acf-field-list-title">
350+
<span class="acf-field-list-title-span"><?php echo $post->post_title; ?> </span>
351+
</div>
352+
353+
<?php
354+
// render input
355+
acf_render_field_wrap( $sub_field, $el );
356+
357+
358+
if ( $field['include_title_editor'] ) {
359+
acf_render_field_wrap( acf_get_valid_field( array(
360+
'name' => "{$field['name']}[{$i}][post_data][post_title]",
361+
'label' => 'Title',
362+
'type' => 'text',
363+
'value' => $post->post_title,
364+
'required' => true
365+
) ), $el );
366+
}
352367

353-
if ( $field['include_content_editor'] ) {
354-
acf_render_field_wrap( acf_get_valid_field( array(
355-
'name' => "{$field['name']}[{$i}][post_data][post_content]",
356-
'label' => __( 'Post Content', 'acf_child_post_field' ),
357-
'type' => 'wysiwyg',
358-
'value' => $post->post_content,
359-
'required' => false
360-
) ), $el );
361-
}
368+
if ( $field['include_content_editor'] ) {
369+
acf_render_field_wrap( acf_get_valid_field( array(
370+
'name' => "{$field['name']}[{$i}][post_data][post_content]",
371+
'label' => __( 'Post Content', 'acf_child_post_field' ),
372+
'type' => 'wysiwyg',
373+
'value' => $post->post_content,
374+
'required' => false
375+
) ), $el );
376+
}
362377

363-
if ( $field['include_excerpt_editor'] ) {
364-
acf_render_field_wrap( acf_get_valid_field( array(
365-
'name' => "{$field['name']}[{$i}][post_data][post_excerpt]",
366-
'label' => __( 'Excerpt', 'acf_child_post_field' ),
367-
'type' => 'textarea',
368-
'value' => $post->post_excerpt,
369-
'required' => false
370-
) ), $el );
371-
}
378+
if ( $field['include_excerpt_editor'] ) {
379+
acf_render_field_wrap( acf_get_valid_field( array(
380+
'name' => "{$field['name']}[{$i}][post_data][post_excerpt]",
381+
'label' => __( 'Excerpt', 'acf_child_post_field' ),
382+
'type' => 'textarea',
383+
'value' => $post->post_excerpt,
384+
'required' => false
385+
) ), $el );
386+
}
372387

373-
if ( $field['include_featured_image_editor'] ) {
374-
acf_render_field_wrap( acf_get_valid_field( array(
375-
'name' => "{$field['name']}[{$i}][post_data][featured_image]",
376-
'label' => __( 'Featured Image', 'acf_child_post_field' ),
377-
'type' => 'image',
378-
'value' => get_post_thumbnail_id( $post->ID ),
379-
'required' => false
380-
) ), $el );
381-
}
388+
if ( $field['include_featured_image_editor'] ) {
389+
acf_render_field_wrap( acf_get_valid_field( array(
390+
'name' => "{$field['name']}[{$i}][post_data][featured_image]",
391+
'label' => __( 'Featured Image', 'acf_child_post_field' ),
392+
'type' => 'image',
393+
'value' => get_post_thumbnail_id( $post->ID ),
394+
'required' => false
395+
) ), $el );
396+
}
382397

383-
foreach ( $field['acf_child_field_fields'] as $child_field ):
398+
foreach ( $field['acf_child_field_fields'] as $child_field ):
384399

385-
// prevent childbuilder field from creating multiple conditional logic items for each row
386-
if ( $i !== 'acfcloneindex' ) {
387-
$child_field['conditional_logic'] = 0;
388-
}
400+
// prevent childbuilder field from creating multiple conditional logic items for each row
401+
if ( $i !== 'acfcloneindex' ) {
402+
$child_field['conditional_logic'] = 0;
403+
}
389404

390405

391-
// add value
392-
if ( isset( $row['acf_child_field_values'][$child_field['key']] ) ) {
393-
// this is a normal value
394-
$child_field['value'] = $row['acf_child_field_values'][$child_field['key']];
395-
} elseif ( isset( $child_field['default_value'] ) ) {
406+
// add value
407+
if ( isset( $row['acf_child_field_values'][$child_field['key']] ) ) {
408+
// this is a normal value
409+
$child_field['value'] = $row['acf_child_field_values'][$child_field['key']];
410+
} elseif ( isset( $child_field['default_value'] ) ) {
396411

397-
// no value, but this sub field has a default value
398-
$child_field['value'] = $child_field['default_value'];
399-
}
412+
// no value, but this sub field has a default value
413+
$child_field['value'] = $child_field['default_value'];
414+
}
400415

401416

402-
// update prefix to allow for nested values
403-
$child_field['prefix'] = "{$field['name']}[{$i}]";
417+
// update prefix to allow for nested values
418+
$child_field['prefix'] = "{$field['name']}[{$i}]";
404419

405420

406-
// render input
407-
acf_render_field_wrap( $child_field, $el );
408-
?>
421+
// render input
422+
acf_render_field_wrap( $child_field, $el );
423+
?>
409424

410-
<?php endforeach; ?>
425+
<?php endforeach; ?>
411426

412-
<?php echo $after_fields; ?>
427+
<?php echo $after_fields; ?>
413428

414-
<?php if ( $show_remove ): ?>
415-
<td class="remove">
416-
<a class="acf-icon small acf-childbuilder-add-row" href="#" data-before="1" title="<?php _e( 'Add row', 'acf_child_post_field' ); ?>"><i class="acf-sprite-add"></i></a>
417-
<a class="acf-icon small acf-childbuilder-remove-row" href="#" title="<?php _e( 'Remove row', 'acf_child_post_field' ); ?>"><i class="acf-sprite-remove"></i></a>
418-
</td>
419-
<?php endif; ?>
429+
<?php if ( $show_remove ): ?>
430+
<td class="remove">
431+
<a class="acf-icon small acf-childbuilder-add-row" href="#" data-before="1" title="<?php _e( 'Add row', 'acf_child_post_field' ); ?>"><i class="acf-sprite-add"></i></a>
432+
<a class="acf-icon small acf-childbuilder-remove-row" href="#" title="<?php _e( 'Remove row', 'acf_child_post_field' ); ?>"><i class="acf-sprite-remove"></i></a>
433+
</td>
434+
<?php endif; ?>
420435

421-
</tr>
422-
<?php endforeach; ?>
436+
</tr>
437+
<?php endforeach; ?>
423438
</tbody>
424439
</table>
425440
<?php if ( $show_add ): ?>
@@ -735,11 +750,11 @@ function update_value( $value, $post_id, $field ) {
735750

736751
if ( $field['include_featured_image_editor'] ) {
737752
$image_id = isset( $row['post_data']['featured_image'] ) ? $row['post_data']['featured_image'] : 0;
738-
update_post_meta($child_post_id, '_thumbnail_id', $image_id);
753+
update_post_meta( $child_post_id, '_thumbnail_id', $image_id );
739754
}
740755
}
741756

742-
757+
743758
// modify name for save
744759
$sub_field['name'] = "{$field['name']}_{$i}_acf_child_field_post_id";
745760
// update field
@@ -799,47 +814,29 @@ function update_value( $value, $post_id, $field ) {
799814
}
800815

801816
/*
802-
* format_value()
803-
*
804-
* This filter is appied to the $value after it is loaded from the db and before it is returned to the template
805-
*
806-
* @type filter
807-
* @since 3.6
808-
* @date 23/01/13
809-
*
810-
* @param $value (mixed) the value which was loaded from the database
811-
* @param $post_id (mixed) the $post_id from which the value was loaded
812-
* @param $field (array) the field array holding all the field options
813-
*
814-
* @return $value (mixed) the modified value
815-
*/
816-
817-
/*
818-
819-
function format_value( $value, $post_id, $field ) {
820-
821-
// bail early if no value
822-
if( empty($value) ) {
823-
824-
return $value;
825-
826-
}
827-
828-
829-
// apply setting
830-
if( $field['font_size'] > 12 ) {
831-
832-
// format the value
833-
// $value = 'something';
834-
835-
}
836-
837-
838-
// return
839-
return $value;
840-
}
841-
842-
*/
817+
* format_value()
818+
*
819+
* This filter is appied to the $value after it is loaded from the db and before it is returned to the template
820+
*
821+
* @type filter
822+
* @since 3.6
823+
* @date 23/01/13
824+
*
825+
* @param $value (mixed) the value which was loaded from the database
826+
* @param $post_id (mixed) the $post_id from which the value was loaded
827+
* @param $field (array) the field array holding all the field options
828+
*
829+
* @return $value (mixed) the modified value
830+
*/
831+
function format_value( $value, $post_id, $field ) {
832+
833+
// bail early if no value
834+
if( empty($value) ) {
835+
return $value;
836+
}
837+
838+
return wp_list_pluck($value, '_acf_child_field_post_id');
839+
}
843840

844841

845842
/*

assets/css/acf-childbuilder-field.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2+
3+
.acf-child-post-field-hide {
4+
display:none;
5+
}
6+
7+
18
/*---------------------------------------------------------------------------------------------
29
*
310
* Post Manager

0 commit comments

Comments
 (0)