• Resolved Mark

    (@markclifford)


    Hello!

    I have a Awards post type and a Nominee post type which is linked together by a relationship custom field. I am currently setting up a form for people to submit a nominee, however the select field needs to pull through the list of awards from that post type in order for it all to be linked. How do I do this I cannot work it out…

    Your help would be much appreciated.

    • This topic was modified 3 years, 2 months ago by Mark.
Viewing 15 replies - 1 through 15 (of 47 total)
  • Plugin Support Nebu John – WPMU DEV Support

    (@wpmudevsupport14)

    Hi @markclifford,

    Trust you are doing good and thank you for reaching out to us.

    I assume that you are looking forward to populating the select fields with posts from the custom post type, please correct me if I am wrong. Can you please check if the following code snippet helps?

    <?php
    
    add_filter(
    	'forminator_cform_render_fields',
    	function( $wrappers, $model_id ) {
    		if( $model_id != 361 ){
    			return $wrappers;
    		}
    
    		$select_fields_data = array(
    			'select-1' => 'post',
    		);
    
    		foreach ( $wrappers as $wrapper_key => $wrapper ) {
    			if ( ! isset( $wrapper[ 'fields' ] ) ) {
    				continue;
    			}
    
    			if ( 
    				isset( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) &&
    				! empty( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] )
    			) {
    				$posts = get_posts( array( 'post_type' => $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) );
    
    				if ( ! empty( $posts ) ) {
    					$new_options = array();
    					$opt_data = array();
    					foreach( $posts as $post ) {
    						$new_options[] = array(
    							'label' => $post->post_title,
    							'value' => $post->post_title,
    							'limit' => '',
    							'key'   => forminator_unique_key(),
    						);
    						$opt_data['options'] = $new_options;
    					}
    					$select_field = Forminator_API::get_form_field( $model_id, $wrapper['fields'][0]['element_id'], true );
    					if( $select_field ){
    						if( $select_field['options'][0]['label'] != $opt_data['options'][0]['label'] ){
    							Forminator_API::update_form_field( $model_id, $wrapper['fields'][0]['element_id'], $opt_data );
    							$wrappers[ $wrapper_key ][ 'fields' ][ 0 ][ 'options' ] = $new_options;
    						}
    					}
    				}
    			}
    		}
    
    		return $wrappers;
    	},
    	10,
    	2
    );
    
    add_filter(
    	'forminator_replace_form_data', 
    	function( $content, $data, $fields ) {
    		if( $data['form_id'] != 361 ){
    			return $content;
    		}
    
    		if ( ! empty( $content ) ) {
    			return $content;
    		}
    
    		$form_fields = Forminator_API::get_form_fields( $data['form_id'] );
    		$data_field = '';
    		foreach($data as $key => $value){
    	    	if ( strpos( $key, 'select' ) !== false ) {
    	    		$values = '';
    	    		$field_value = isset( $data[ $key ] ) ? $data[ $key ] : null;
    
    		    	if ( ! is_null( $field_value ) ) {
    		    		$fields_slugs  = wp_list_pluck( $form_fields, 'slug' );
    					$field_key     = array_search( $key, $fields_slugs, true );
    					$field_options = false !== $field_key && ! empty( $form_fields[ $field_key ]->raw['options'] )
    							? wp_list_pluck( $form_fields[ $field_key ]->options, 'label', 'value' )
    							: array();
    
    					if ( ! isset( $field_options[ $field_value ] ) && isset( $_POST[ $key ] ) ) {
    						return sanitize_text_field( $_POST[ $key ] );
    					}
    				}
    			}
    		}
    	    return $content;
    	},
    	10,
    	3
    );

    Please note, you need to update the form ID (361) in the above code and replace the posts from the following line with your custom post type.

    'select-1' => 'post',

    Please test the code using a dev/staging environment before pushing it to the live website. The code could be added using a mu-plugin, please find more details here: https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    Please feel free to get back to us if you need any further clarification.

    Kind Regards,
    Nebu John

    Thread Starter Mark

    (@markclifford)

    Hey @wpmudevsupport14! Thanks for that! Done as requested with changing ID and post type of the Awards categories https://a.cl.ly/geuERoWp

    However, the form disappears from the page and I get the following query appear (which is a custom query within Themeco’s “PRO” theme: https://a.cl.ly/xQuRwYL9

    Plugin Support Laura – WPMU DEV Support

    (@wpmudev-support8)

    Hi @markclifford

    Unfortunately, I don’t have this theme to test the code/form with it (as it’s a third-party premium theme) but it certainly is not correct behavior of the page and the query should not show up on page.

    It actually looks like some kind of syntax error, because the code is thrown directly to the page (printed out) instead of being executed.

    That said:

    1. I understand that if you remove the code that my colleague shared – the issue goes away, right?

    2. to make sure – the issue is happening “right away” when you visit the page with the form or after you attempt to submit the form only?

    3. could you share screenshot of the entire custom code (the one we shared but after your modifications – “as is”, how you added it)? Also, did you add it as MU plugin or, by any chance, put in theme’s functions PHP file or some kind of theme’s “custom code” Option?

    Kind regards,
    Adam

    Thread Starter Mark

    (@markclifford)

    Hello again,

    I fixed the query string, it wasn’t anything to do with what we’re doing here thankfully.

    1. I understand that if you remove the code that my colleague shared – the issue goes away, right?

    No, the error has been resolved now so just need to get the code working and select field populating with the categories now.

    2. to make sure – the issue is happening “right away” when you visit the page with the form or after you attempt to submit the form only?

    Form submits fine but the select field is not populated with custom post type “categories”

    3. could you share screenshot of the entire custom code (the one we shared but after your modifications – “as is”, how you added it)? Also, did you add it as MU plugin or, by any chance, put in theme’s functions PHP file or some kind of theme’s “custom code” Option?

    Code: https://a.cl.ly/RBurmABy
    File location: https://a.cl.ly/L1uDXb6P

    Plugin Support Jair – WPMU DEV Support

    (@wpmudevsupport15)

    Hi @markclifford,

    I hope you are doing well today!

    Please share the code as text instead of images, also it would be great if you can share an export of the form which will help us more.

    You can find more info on how to export the form here : https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#import-export

    After exporting, please share the form code using a service such as https://pastebin.com which is free to use.

    Please always make sure to use such service to share the codes and don’t post the code here directly as it will most likely be unusable.

    Kind regards,
    Zafer

    Thread Starter Mark

    (@markclifford)

    Here you go Zafer.

    Form export: https://pastebin.com/RtzEgGdX
    Code: https://pastebin.com/67UjVaFC

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @markclifford,

    In the given code, I see you are using the following:

    $select_fields_data = array(
    			'select-1' => 'categories',
    		);

    The above usage isn’t correct. So just to be sure that we understand the workflow you have.

    The Nominee is a custom post type and it has a category called “Awards”? And you want to list all the Awards categories in the Forms Select field, right?

    How is the Nominee post type created? Is that part of the theme, custom code or plugin? Could you also please share a screenshot of the mentioned post types you have on your side so that we could have a better idea?

    Also please share the URL of the pages of the Nominee post type and the category page ie the URL after /wp-admin/ in your WP Dashboard, so that it would help us to determine the correct slugs that needs to be used in the code.

    Looking forward to your response.

    Kind Regards,

    Nithin

    Thread Starter Mark

    (@markclifford)

    Hello @wpmudevsupport11

    I did explain it clearly I thought but perhaps further clarification would be helpful here.

    Awards Category is wp-admin/edit.php?post_type=categories (Awards) categories
    Nominees is wp-admin/edit.php?post_type=nominee

    These are linked with a relationship field within Custom Fields Pro. All custom post types are created with CPT UI.

    I would like the Awards (categories) to be populated within a select field in Forminator.

    • This reply was modified 3 years, 2 months ago by Mark.
    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @markclifford

    I hope you are doing well.

    Let me explain how it works because it is a 2-steps solution.

    First of all, we need to create our Select field in Forminator with the options, the shared code will do that for you dynamically, it will list all the Categories into that Select removing the need to manually update the form if you add or remove any new category.

    But this is optional, I imported your field and I can see you are adding the options manually.

    The second part is the mapping itself, Forminator has out-of-the-box mapping with custom fields which will work with ACF, for example, input, textarea or others, but some complex relationships may require a code.

    I tried to duplicate this behaviour on my lab site and I can confirm the mapping will work well with some updates on your form.

    – Edit the Post data, map the select and your ACF Field:

    https://monosnap.com/file/deKRvDZdd2QOt2Md3XKEjOgVHCVAgV

    Make sure the first field matches the ACF field name.
    Edit your Select Field, use the Value the Post item ID:

    https://monosnap.com/file/oHmrcAENSBznJ3qH1XcEBATMy3bIi0
    https://monosnap.com/file/f1XI8WngQONGA7OKFxovGKBdu0kYel

    Now it will work out of the box:

    https://monosnap.com/file/cE6VyH7zSgPj4WwwxiumavLLlfnIaU

    With the first shared code, you can make it more dynamic but also requires an update on

    $new_options[] = array(
     ...
    'value' => $post->post_title,
    ....

    To

    $new_options[] = array(
     ...
    'value' => $post->ID,
    ....

    Because the Post ID is which will do the mapping.

    Let us know if you have any additional questions.

    Best Regards
    Patrick Freitas

    Thread Starter Mark

    (@markclifford)

    @wpmudevsupport12 It is not pulling through all 8 awards despite following all steps above. Would you like admin access?

    Plugin Support Laura – WPMU DEV Support

    (@wpmudev-support8)

    Hi @markclifford

    Please replace this line in code

    $posts = get_posts( array( 'post_type' => $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) );

    with this code

    $p_args = array(
    	'post_type' =>  $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ],
    	'numberposts' => -1,
    );
    
    $posts = get_posts( $p_args );

    The difference is that the original line uses get_post with no post number specified so it will always follow current site settings or whatever other code custom-set post number “meanwhile”.

    The new version specifically sets number of posts to -1 value which means “all posts”.

    Kind regards,
    Adam

    Thread Starter Mark

    (@markclifford)

    Ok. I think thats working… I have also renamed the class so that I can target it for auto populating the select field.

    I have a few other things which you may be able to help with…

    1. Text area title seems to be hidden behind the field? https://a.cl.ly/Jrupeek8

    2. I need to make sure “Awards Year” which is a tag within the Awards category is applied to nominees. For example this years nominees would be tagged with “2023”. How do I do this without giving the person the option to choose which year?

    Thread Starter Mark

    (@markclifford)

    Unfortunately, I have discovered that on submitting the form the information is not passed correctly. For example, the categories and awards year isn’t recorded.

    https://a.cl.ly/P8u2XDzq

    Plugin Support Laura – WPMU DEV Support

    (@wpmudev-support8)

    Hi @markclifford

    I’m glad it’s working.

    As for your questions:

    1. Text area title seems to be hidden behind the field? https://a.cl.ly/Jrupeek8

    Please start a separate ticket about it as it’s a different issue.

    2. I need to make sure “Awards Year” which is a tag within the Awards category is applied to nominees. For example this years nominees would be tagged with “2023”. How do I do this without giving the person the option to choose which year?

    This could be done with another bit of additional code (you can actually append it to your custom code at the very end of the file, or you can add opening PHP tag and add it in separate .php file):

    add_action( 'forminator_post_data_field_post_saved', 'forminator_post_data_field_post_saved_custom_tag', 11, 3 );
    function forminator_post_data_field_post_saved_custom_tag( $post_id, $field, $data ) { 
    
    	$cats = array( 111 ); // category IDs, one or more comma separated; posts in this category will be tagged
    	$tags = array( 169 ); // define tags IDs - one or more, comma spearated
    	$tax = 'post_tag'; // name of taxonomy tag to be used, post_tag is default
    	
    	if ( in_array( $data['category'], $cats ) ) {
    				
    		wp_set_post_terms( $post_id, $tags, $tax );
    		
    	}
    }

    All you’d need to do in the code would be to configure it in these three lines:

    a) replace number 111 with a numberical ID of the category in which created posts should be tagged; you can use more than one, just separate numbers with comma.

    $cats = array( 111 ); // category IDs, one or more comma separated; posts in this category will be tagged

    b) replace number 169 with numerical ID of (existing) tag that should be added; again, you can add more than one, just separate them with commas

    $tags = array( 169 ); // define tags IDs - one or more, comma spearated

    c) finally, you need to specify the taxonomy name here; in this example I used “post_tag” which is default for standard post tags; but for custom posts it may be different, depending on how you named/created them

    $tax = 'post_tag'; // name of taxonomy tag to be used, post_tag is default

    And that’s it. This code doesn’t apply if you manually create the post in with editor on site – only if it’s created via the Forminator form. It will then check if the post is in a defined category and if so, set defined tags (taxonomy terms) for it.

    Best regards,
    Adam

    Thread Starter Mark

    (@markclifford)

    Thats not working for me at all. In fact, when submitting the form the post isn’t being linked with the award nor the award year https://a.cl.ly/P8u2XDzq

    I’d be more than happy to give you access to the site to have a deeper look.

    <?php
    
    add_action( 'awards', 'awards', 11, 3 );
    function awards( $post_id, $field, $data ) { 
    
    	$cats = array( 434,433 ); // category IDs, one or more comma separated; posts in this category will be tagged
    	$tags = array( 3 ); // define tags IDs - one or more, comma separated
    	$tax = 'awards_year'; // name of taxonomy tag to be used, post_tag is default
    	
    	if ( in_array( $data['category'], $cats ) ) {
    				
    		wp_set_post_terms( $post_id, $tags, $tax );
    		
    	}
    }
    
    ?>
    <?php
    
    add_filter(
    	'awards',
    	function( $wrappers, $model_id ) {
    		if( $model_id != 263 ){
    			return $wrappers;
    		}
    
    		$select_fields_data = array(
    			'select-1' => 'categories',
    		);
    
    		foreach ( $wrappers as $wrapper_key => $wrapper ) {
    			if ( ! isset( $wrapper[ 'fields' ] ) ) {
    				continue;
    			}
    
    			if ( 
    				isset( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) &&
    				! empty( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] )
    			) {
    				$p_args = array('post_type' =>  $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ], 'numberposts' => -1, );
    
    $posts = get_posts( $p_args );
    
    				if ( ! empty( $posts ) ) {
    					$new_options = array();
    					$opt_data = array();
    					foreach( $posts as $post ) {
    						$new_options[] = array(
    							'label' => $post->post_title,
    							'value' => $post->ID,
    							'limit' => '',
    							'key'   => forminator_unique_key(),
    						);
    						$opt_data['options'] = $new_options;
    					}
    					$select_field = Forminator_API::get_form_field( $model_id, $wrapper['fields'][0]['element_id'], true );
    					if( $select_field ){
    						if( $select_field['options'][0]['label'] != $opt_data['options'][0]['label'] ){
    							Forminator_API::update_form_field( $model_id, $wrapper['fields'][0]['element_id'], $opt_data );
    							$wrappers[ $wrapper_key ][ 'fields' ][ 0 ][ 'options' ] = $new_options;
    						}
    					}
    				}
    			}
    		}
    
    		return $wrappers;
    	},
    	10,
    	2
    );
    
    add_filter(
    	'forminator_replace_form_data', 
    	function( $content, $data, $fields ) {
    		if( $data['form_id'] != 263 ){
    			return $content;
    		}
    
    		if ( ! empty( $content ) ) {
    			return $content;
    		}
    
    		$form_fields = Forminator_API::get_form_fields( $data['form_id'] );
    		$data_field = '';
    		foreach($data as $key => $value){
    	    	if ( strpos( $key, 'select' ) !== false ) {
    	    		$values = '';
    	    		$field_value = isset( $data[ $key ] ) ? $data[ $key ] : null;
    
    		    	if ( ! is_null( $field_value ) ) {
    		    		$fields_slugs  = wp_list_pluck( $form_fields, 'slug' );
    					$field_key     = array_search( $key, $fields_slugs, true );
    					$field_options = false !== $field_key && ! empty( $form_fields[ $field_key ]->raw['options'] )
    							? wp_list_pluck( $form_fields[ $field_key ]->options, 'label', 'value' )
    							: array();
    
    					if ( ! isset( $field_options[ $field_value ] ) && isset( $_POST[ $key ] ) ) {
    						return sanitize_text_field( $_POST[ $key ] );
    					}
    				}
    			}
    		}
    	    return $content;
    	},
    	10,
    	3
    );
    ?> 
    • This reply was modified 3 years, 1 month ago by Mark.
    • This reply was modified 3 years, 1 month ago by Mark.
    • This reply was modified 3 years, 1 month ago by Mark.
Viewing 15 replies - 1 through 15 (of 47 total)

The topic ‘Linking ACF Select with Forminator Select Field’ is closed to new replies.