facetwp custom field in layout builder with attachment id

djrmom

{{gist file=”custom-hooks.php” lang=”php”}}
<?php
/** for using a custom field that saves an image as attachment_id instead of url or path
** in the layout builder select the field and give it a unique in the advanced tab https://d.pr/i/xpD9p3
**/

add_filter( ‘facetwp_builder_item_value’, function( $value, $item ) {
if ( ‘my-image’ == $item[‘settings’][‘name’] && !empty( $value ) ) { // change my-image to the unique name of your field
$value = wp_get_attachment_image( $value, ‘full’ ); // ‘full’ can be changed to other image sizes you have available
}
return $value;
}, 10, 2 );

/** you can also use the source which is generally the custom field name if you know it
** but some custom field plugins create names which may be harder to find
**/
add_filter( ‘facetwp_builder_item_value’, function( $value, $item ) {
if ( ‘my_custom_image_field’ == $item[‘source’] && !empty( $value ) ) { // change ‘my_custom_image_field’ to the unique name of your field
$value = wp_get_attachment_image( $value, ‘full’ ); // ‘full’ can be changed to other image sizes you have available
}
return $value;
}, 10, 2 );
{{/gist}}