support.cubewp.com support.cubewp.com
  • Helpdesk
  • Support Forum
Get CubeWP
support.cubewp.com support.cubewp.com
Get CubeWP
support.cubewp.com
  • Helpdesk
  • Support Forum

Docy

CubeWP Framework

  • getting startGetting Started
    • Introduction
    • Installation
    • Support
    • Billing
  • custom post typeCustom Post Types
  • custom taxonomiesCustom Taxonomies
  • Custom Fields
    • Custom Fields for Post Types
    • Custom Fields for Taxonomies
    • Custom Fields for User Roles
    • Custom Field for Settings
  • Custom Field Types
  • Search Forms
  • search filterSearch Filters
  • setting iconCubeWP Settings
  • exportExport
  • importImport
  • Shortcodes
    • Login Page
    • Custom Field Shortcode
    • Archive Shortcodes
  • GuideGuides
    • How to create Custom archive page?
    • Maps in CubeWP
    • Conditional Fields with Taxonomy
    • Conditional Custom Fields
    • Dropdown Field
    • Checkbox Field
    • Radio Button Field
    • How to create Repeatable Fields ?
    • Conditional Custom Fields with Multiple Taxonomies
    • How to Allow Specific File Types in Media?
    • Relationship Fields
    • Custom CSS on Custom Fields
  • Developer Guides
    • How to Get Data Using WP Rest API in CubeWP?
    • How to add any other post type into CubeWP form builder.
    • How to get value of post-type custom field
    • How to get CubeWP Setting’s field value
    • How to create group of custom fields by code
    • CubeWP Filters & Actions
  • faqFAQs
    • What is CubeWP Framework?
    • Does AppSumo deal include all future extensions?
    • Does CubeWP work with any page builder?
    • How can I display custom field data on a single-post template using any page builder?
    • Does CubeWP compatible with Elementor Dynamic Tags?
    • Is CubeWP compatible with any WordPress theme as well?
    • Does CubeWP work with native WP Gutenberg blocks and 3rd party Guten blocks?
    • Does CubeWP support Full Site Editing (FSE)?
    • Is CubeWP Frontend Pro a Page Builders?
    • Is the CubeWP Framework safe to use on WordPress?
    • Can I create custom post types and taxonomies with CubeWP?
    • Can I create custom fields with CubeWP?
    • Is CubeWP compatible with WooCommerce?
    • How can I translate my CubeWP powered site to another language?
    • Is CubeWP SEO friendly?
    • Can I use CubeWP to build a membership site?
    • Is CubeWP compatible with LearnDash or other LMS plugins?
    • Can I create a multivendor marketplace with CubeWP?
    • Can a Non Developer use CubeWP Framework?
    • How do I get support for CubeWP?
    • What are the minimum requirements?
    • Can I use CubeWP without Elementor?
    • Does CubeWP have a refund policy?
    • Is CubeWP WordPress Multisite compatible?
    • Which page builders are compatible?
    • Is the current CubeWP deal on AppSumo a Lifetime Deal?
    • Which themes are compatible?
    • Can I create custom Gutenberg blocks with CubeWP?
    • Does CubeWP work with my WordPress Theme?
    • How often is CubeWP updated?
    • Does CubeWP support Shortcodes?
    • Is CubeWP GDPR-compliant?
    • Does CubeWP work with e-commerce plugins?
    • Does CubeWP offer any built-in caching or performance optimization features?
    • How many Field Types are available in CubeWP?
    • Why my Conditional Logic is not working while creating Custom Fields?
    • Can I export or import my CubeWP settings?
    • Are there any known conflicts with other WordPress plugins?
    • Where can I see all my purchases?
    • Can I Import and Export data from CubeWP?
    • Can I use CubeWP on my clients’ websites?
    • How to activate and manage licenses?
    • Does CubeWP have a refund policy?
    • Does CubeWP support Elementor Dynamic Tag?
    • Are there any live example of customer websites built with CubeWP?
    • Is there a demo website for CubeWP?
    • Do I need Elementor Pro for Dynamic Tags?
    • What is CubeWP Frontend Pro?
    • Is CubeWP Frontend Pro a Page Builder?

How to create group of custom fields by code

Estimated reading: 2 minutes 929 views

You can use below given code for creating group of fields,

Example:

// Put this code in your functions.php or any other file where you want to run this according to your need.
add_action( 'admin_init', 'create_post_types_custom_fields_group' );


function create_post_types_custom_fields_group() {
        // Seprated by comma if you want to assign this group of fields to multiple post types.
	$post_types = 'your_post_type'; 
       // It should be string.
	$group_name = 'your_group_name'; 
	$postData = get_posts( array(
		'name'        => $group_name,
		'post_type'   => 'cwp_form_fields',
		'post_status' => 'publish',
		'fields'      => 'id',
		'numberposts' => 1
	) );
	$post_id  = count( $postData ) > 0 ? $postData[0]->ID : '';
	// Create post object
	if ( empty( $post_id ) ) {
		if ( ! $post_id ) {
			$my_post = array(
				'post_title'   => __( 'Your Group Title', 'Your-Text-Domain' ),
				'post_name'    => $group_name,
				'post_content' => 'mandatory general custom fields.',
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_type'    => 'cwp_form_fields'
			);
			// Insert the group into the database
			$post_id = wp_insert_post( $my_post );
                        // if this meta is set as secure then this group of field will not have delete option.
			update_post_meta( $post_id, '_cwp_group_visibility', 'secure' ); 
			update_post_meta( $post_id, '_cwp_group_types', $post_types );
			update_post_meta( $post_id, '_cwp_group_order', 1 );
		}
		$fields  = post_types_group_custom_fields( $post_id );
		$custom_fields = array();
		foreach ( $fields as $key => $field ) {
			$custom_fields[] = $key;
			CubeWp_Custom_Fields_Processor::set_option( $key, $field );
		}
		update_post_meta( $post_id, '_cwp_group_fields', implode( ',', $custom_fields ) );
	}
}

// This function will return array of custom fields.
function post_types_group_custom_fields( $post_id ) {
	$fields = array();
	$fields['price']   = array(
		'label'                => __( 'Price', 'Your-Text-Domain' ),
		'name'                 => 'price',
		'type'                 => 'number',
		'description'          => '',
		'default_value'        => '',
		'placeholder'          => '',
		'options'              => json_encode( array() ),
		'filter_post_types'    => '',
		'filter_taxonomy'      => '',
		'filter_user_roles'    => '',
		'appearance'           => '',
		'required'             => 1,
		'validation_msg'       => __( 'Price field is mandatory', 'Your-Text-Domain' ),
		'id'                   => 'price_id',
		'class'                => '',
		'container_class'      => '',
		'conditional_operator' => '!empty',
		'conditional_value'    => '',
		'group_id'             => $post_id
	);
	$fields['gender'] = array(
		'label'                => __( 'Gender', 'Your-Text-Domain' ),
		'name'                 => 'gender',
		'type'                 => 'radio',
		'description'          => '',
		'default_value'        => '',
		'placeholder'          => '',
		'options'              => json_encode( array(
			'label' => array(
				__( 'Male', 'Your-Text-Domain' ),
				__( 'Female', 'Your-Text-Domain' )
			),
			'value' => array( 'male', 'female' )
		) ),
		'filter_post_types'    => '',
		'filter_taxonomy'      => '',
		'filter_user_roles'    => '',
		'appearance'           => '',
		'required'             => '',
		'validation_msg'       => '',
		'id'                   => 'gender_id',
		'class'                => '',
		'container_class'      => '',
		'conditional_operator' => '!empty',
		'conditional_value'    => '',
		'group_id'             => $post_id
	);
	return $fields;
}
Share this Doc

How to create group of custom fields by code

Or copy link

Clipboard Icon
CONTENTS
Leaf Illustration

© 2023 All Rights Reserved by CubeWP