gform_advancedpostcreation_meta_fields

Description

The gform_advancedpostcreation_meta_fields filter is used to modify the default custom fields that is shown in Custom Fields in a Feed. This filter allows developers to modify the custom fields globally or a specific form.

Note: New custom fields that have not been used on the site will not be shown in the Custom Fields.

Usage

For all forms

add_filter( 'gform_advancedpostcreation_meta_fields', 'your_function_name', 10, 4 );

For a specific form

add_filter( 'gform_advancedpostcreation_meta_fields_{$FORM_ID}', 'your_function_name', 10, 4 );

Parameters

ParameterTypeDescription
$meta_keysarrayThe array of custom field meta keys to be displayed.
$formForm ObjectThe current form.
$feedFeed ObjectThe current feed.
$post_typestringThe post type associated with the feed.

Examples

Set Fields to be Specific Prefix on a Specific Form

add_filter( 'gform_advancedpostcreation_meta_fields_23', 'book_fields_only', 10, 4 );
function book_fields_only( $meta_keys, $form, $feed, $post_type ) {
	$book_post_fields = array_filter($meta_keys, fn($item) => str_starts_with($item, 'book_'));
	return $book_post_fields;
}

Metabox: Set Custom Fields from CPT First

Regardless of which post type is chosen on the Feed setting, as long as it’s made with Metabox, the fields will appear on the top in Custom Fields for all forms.

add_filter( 'gform_advancedpostcreation_meta_fields', 'get_all_mb_fields', 10, 4 );
function get_all_mb_fields( $meta_keys, $form, $feed, $post_type ) {
  $pre_meta_keys = array();

  if ( function_exists( 'rwmb_get_registry' ) ) {
    $meta_box_registry = \rwmb_get_registry( 'meta_box' );
    $meta_boxes        = $meta_box_registry->get_by( [ 'object_type' => 'post', 'post_types' => [ $post_type ] ] );

    if ( is_array( $meta_boxes ) && count( $meta_boxes ) === 1 ) {
      foreach ( $meta_boxes as $meta_box ) {
        if ( ! is_object( $meta_box ) ) {
          continue;
        }
        
        if ( ! property_exists( $meta_box, 'meta_box' ) ) {
          continue;
        }
        
        foreach ( $meta_box->meta_box['fields'] as $field ) {
          $pre_meta_keys[] = $field['id'];
        }
    
        $meta_keys = array_diff( $meta_keys, $pre_meta_keys );
      }
    }
  }

  return array_merge( $pre_meta_keys, $meta_keys );
}

Placement

This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.

See also the PHP section in this article: Where Do I Put This Code?

Source Code

This filter is located in class-gf-advancedpostcreation.php

Since

This filter was added in