Overview
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 );
Supercharge Your Website With Premium Features Using ACF PRO
Speed up your workflow and unlock features to better develop websites using ACF Blocks and Options Pages, with the Flexible Content, Repeater, Clone, Gallery Fields & More.
Related
- Filters: acf/schema/block_field_objects
- Filters: acf/schema/format_value/pre
- Filters: acf/schema/output_format_choices
- Filters: acf/schema/format_value
- Filters: acf/schema/post_field_objects/post_type={post_type}