Custom Fields
Re-Envisioned
Support General Display element based on 'post'

This topic contains 42 replies, has 3 voices, and was last updated by  carassius 4 years ago.

  • Creator
    Topic
  • #1306

    carassius
    Participant

    If I create a basic customer cpt
    name
    phone
    email
    address

    and then in another cpt use ‘type’ => ‘post’ to generate a drop down of names, how can I then filter the other components based on the selected name? Should I use the conditional extension, or is there a way to do this without?

    For example, if someone selects “john doe” I then want the other contact info displayed in input boxes. I hope that makes sense?

Viewing 10 replies - 11 through 20 (of 42 total)
  • Author
    Replies
  • #1329

    Tan Nguyen
    Participant

    Ok man, sorry, in my demo, I use ‘customer’ as post type but in your site, you’re using ‘p52-contact’ as post type so $customer variable return empty, can you please change ‘customer’ to ‘p52-contact’?

    #1335

    carassius
    Participant

    Thanks Tan, I am still trying to break down your code and get it to work, can I ask another question?

    In my contact post type, my prefix for the phone is p52_contact_ to give me the id p52_contact_phone, and given that nothing is working (field not being shown nor any value in it) I am assuming something in here is wrong

    // Save data to phone field
    add_action('rwmb_before_save_post', function($post_id)
    {
    	// Get person ID to save from "Select a Customer" field
    	$person_to_save = intval( $_POST['p52_quote_name'] );
    	// Save related field to phone field
    	$_POST['p52_quote_phone'] = $_POST['p52_quote_phone_' . $person_to_save];
    	// Unset all hidden fields
    	foreach ( $_POST as $key => $value )
    	{
    		if ( strpos( $key, 'p52_quote_phone_' ) )
    			unset( $_POST[$key] );
    	}
    } );
    #1344

    Tan Nguyen
    Participant

    Hi carassius

    To explain it more clearly, I’ve basically created two fields, as defined in this array:

    
    'fields' => array(
    			array(
    				'name' => 'Name',
    				'id' => $prefix . 'name',
    				'type' => 'post',
    				'post_type' => 'customer',
    				'placeholder' => 'Select a Customer'
    			),
    			// Real field
    			array(
    				'name' 		=> 'Phone',
    				'id' 		=> $prefix . 'phone',
    				'type' 		=> 'hidden'
    			)
    		)
    

    There’re two fields called p52_quote_name which type is is post and p52_quote_phone which is hidden field, this field will hold the real value. ($prefix = ‘p52_quote_’ as you defined)

    I’ve also created a bunch of input text fields, each field has ID: p52_quote_phone_$customer_id which customer ID is the post ID, and the default value is the customer phone, as this snippet:

    
    foreach ( $customers as $customer )
    	{
    		// Show this phone field if match name.
    		$meta_box['fields'][] = array(
    			'name' 		=> 'Phone',
    			'id' 		=> $prefix . 'phone_' . $customer->ID,
    			'type' 		=> 'text',
    			'std' 		=> get_post_meta( $customer->ID, 'p52_quote_phone', true ),
    			'visible' 	=> array( $prefix . 'name', $customer->ID )
    		);
    	}
    

    You can see

    
    'visible' 	=> array( $prefix . 'name', $customer->ID )
    

    That means, visible this field, when the select customer name is equal to customer ID, that means if we have two persons with phones, for example called:

    
    ID 1: Jim : 0123456789
    ID 2: Kane: 0139401451
    

    So, if Jim, ID 1 (p52_quote_name) is selected, the field, p52_quote_phone_1 will visible with default value defined in std is: 0123456789

    This snippet wich you’ve asked is to remove all faked text fields defined above, and store visible field (p52_quote_phone_1) to p52_quote_phone field 🙂

    
    // Save data to phone field
    add_action('rwmb_before_save_post', function($post_id)
    {
    	// Get person ID to save from "Select a Customer" field
    	$person_to_save = intval( $_POST['p52_quote_name'] );
    	// Save related field to phone field
    	$_POST['p52_quote_phone'] = $_POST['p52_quote_phone_' . $person_to_save];
    	// Unset all hidden fields
    	foreach ( $_POST as $key => $value )
    	{
    		if ( strpos( $key, 'p52_quote_phone_' ) )
    			unset( $_POST[$key] );
    	}
    } );
    

    If you still have problem, can you give me your site information to debug?

    #1345

    carassius
    Participant

    I keep getting that foreach error on line 81

    https://pastebin.com/Wr6E0kmA

    #1346

    Tan Nguyen
    Participant

    Did you have anoter Group field or other Meta Boxes?

    #1347

    carassius
    Participant

    Yes, I have loads.

    This is the full code for my quote cpt

    https://pastebin.com/YiLH0HWv

    #1348

    carassius
    Participant

    If it is to hard, or beyond the scope of support to help me out, just let me know, no probs

    Thank You for everything so far, I do appreciate your time.

    Jon

    #1349

    Tan Nguyen
    Participant

    Hi Jon, I found another problem on your code:

    
    // start customer
    $meta_boxes[] = array(
    

    You can remove [], it just $meta_box.

    On step value, can you please change to full value contains zero sign, 0.01 instead of ‘step’ => .01?

    If you still get error, try to comment out some other Meta Box (I though is Groups), to check what meta boxes generate error 🙂

    #1350

    carassius
    Participant

    removing square brackets, [] breaks my site completely

    #1351

    Tan Nguyen
    Participant

    I means only remove square bracket only with $meta_box below, like my example in the previous page.

    
    // start customer
    
Viewing 10 replies - 11 through 20 (of 42 total)

The topic ‘Display element based on 'post'’ is closed to new replies.