acf/schema/post_field_objects
Filter field objects before processing for a post.
Description
This filter allows you to modify or replace the field objects that are processed for JSON-LD output on posts. Return a non-null value to override the default field retrieval via get_field_objects().
This is useful for adding custom data sources, filtering out specific fields, or providing field data from alternative sources.
Changelog
- Added in version 6.8.0
Parameters
apply_filters( 'acf/schema/post_field_objects', $field_objects, $post, $post_type );
$field_objects(array|null) Field objects array, or null to use default retrieval$post(WP_Post) The post object$post_type(string) The post type name
Return
(array|null) Modified field objects array, or null to use default behavior.
Example
This example adds a virtual field for calculated data.
/**
* Add calculated field to JSON-LD output.
*
* @param array|null $field_objects The field objects array.
* @param WP_Post $post The post object.
* @param string $post_type The post type.
* @return array Modified field objects.
*/
function my_acf_schema_add_calculated_field( $field_objects, $post, $post_type ) {
// Get default field objects if not already set
if ( null === $field_objects ) {
$field_objects = get_field_objects( $post->ID, false );
}
if ( ! is_array( $field_objects ) ) {
$field_objects = array();
}
// Add calculated total time for recipes
if ( $post_type === 'recipe' ) {
$prep_time = get_field( 'prep_time', $post->ID );
$cook_time = get_field( 'cook_time', $post->ID );
$field_objects['total_time'] = array(
'value' => $prep_time + $cook_time,
'schema_property' => 'totalTime',
'type' => 'number',
);
}
return $field_objects;
}
add_filter( 'acf/schema/post_field_objects', 'my_acf_schema_add_calculated_field', 10, 3 );
This example excludes specific fields from JSON-LD output.
/**
* Exclude internal fields from JSON-LD output.
*
* @param array|null $field_objects The field objects array.
* @param WP_Post $post The post object.
* @param string $post_type The post type.
* @return array Modified field objects.
*/
function my_acf_schema_exclude_internal_fields( $field_objects, $post, $post_type ) {
if ( null === $field_objects ) {
$field_objects = get_field_objects( $post->ID, false );
}
if ( ! is_array( $field_objects ) ) {
return $field_objects;
}
// Remove fields starting with underscore
foreach ( $field_objects as $key => $field ) {
if ( strpos( $key, '_' ) === 0 ) {
unset( $field_objects[ $key ] );
}
}
return $field_objects;
}
add_filter( 'acf/schema/post_field_objects', 'my_acf_schema_exclude_internal_fields', 10, 3 );
这篇文档对你有帮助吗?
谢谢你的反馈!