Last active
March 24, 2025 17:38
-
-
Save davidallenlewis/f5214396078c0ed82ac998b5f87da26e to your computer and use it in GitHub Desktop.
Get ACF metadata into the Block Editor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* WordPress dependencies | |
*/ | |
import { addFilter } from '@wordpress/hooks'; | |
import { createHigherOrderComponent } from '@wordpress/compose'; | |
import { Fragment } from '@wordpress/element'; | |
import { useEntityProp } from '@wordpress/core-data'; | |
/** | |
* Add controls to the block editor | |
*/ | |
const withInspectorControl = createHigherOrderComponent( (BlockEdit) => { | |
return ( props ) => { | |
const { | |
attributes: { query, relatedMode }, | |
context: { postType, postId }, | |
} = props; | |
// Get ACF Postmeta | |
const [ meta, updateMeta ] = useEntityProp( | |
'postType', postType, | |
'meta', postId | |
); | |
// Get the related posts | |
let relatedPostIds = [{}]; | |
if( relatedMode === 'postresource_postresource' ) { | |
relatedPostIds = meta?.related_postresource_postresource_json; | |
} else { | |
relatedPostIds = meta?.related_person_postresource_json; | |
} | |
return ( | |
<Fragment> | |
{ relatedPostIds && ( | |
relatedPostIds.map( ( relatedPost ) => ( | |
<Fragment>{ relatedPost.name }</Fragment> | |
) ) | |
) } | |
); | |
}; | |
}, 'withInspectorControl'); | |
addFilter( | |
'editor.BlockEdit', | |
'block-mods/variation-query-loop-block/add-inspector-controls', | |
withInspectorControl, | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Register Custom Field to store our ACF metadata in the REST API | |
*/ | |
function register_post_meta_fields() { | |
register_post_meta( '', 'related_postresource_postresource_json', | |
array( | |
'single' => true, | |
'type' => 'array', | |
'show_in_rest' => array( | |
'schema' => array( | |
'type' => 'array', | |
'items' => array( | |
'type' => 'object', | |
'properties' => array( | |
'id' => array( | |
'type' => 'number' | |
), | |
'name' => array( | |
'type' => 'string' | |
), | |
'url' => array( | |
'type' => 'string', | |
'format' => 'uri' | |
), | |
), | |
), | |
), | |
'prepare_callback' => function( $value ) { | |
$post_ids = get_field( 'related_postresource_postresource', get_the_ID() ) ?? '' ; | |
if( $post_ids ) { | |
foreach ($post_ids as $post_id) { | |
$posts[] = (object) array( | |
'id' => $post_id, | |
'name' => get_the_title( $post_id ), | |
'url' => get_the_permalink( $post_id ), | |
); | |
} | |
return $posts; | |
} | |
} | |
), | |
) | |
); | |
} | |
add_action( 'init', 'register_post_meta_fields' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* WordPress dependencies | |
*/ | |
import assign from 'lodash/assign'; | |
import merge from 'lodash/merge'; | |
import { addFilter } from '@wordpress/hooks'; | |
/** | |
* Add toggle diabled attribute to details block + text alignment support | |
* | |
* @param {Object} settings Original block settings | |
* @param {string} name Block name | |
* @return {Object} Filtered block settings | |
NOTE: Get this to provide context like "is related variation" and have post-template consume that | |
*/ | |
function addCustomAttributes(settings, name) { | |
if ( name === 'core/query' ) { | |
return assign({}, settings, { | |
usesContext: merge(settings.usesContext, [ | |
'postId', | |
'postType', | |
]), | |
attributes: merge(settings.attributes, { | |
...settings.attributes, | |
relatedMode: { | |
type: 'string', | |
}, | |
query: { | |
postId: { | |
type: 'number', | |
}, | |
relatedMode: { | |
type: 'string', | |
}, | |
} | |
}), | |
}); | |
} | |
return settings; | |
} | |
addFilter( | |
'blocks.registerBlockType', | |
'block-mods/query-block/add-custom-attributes', | |
addCustomAttributes, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment