Display element based on 'post'

Support General Display element based on 'post'

Viewing 15 posts - 1 through 15 (of 43 total)
  • Author
    Posts
  • #1306
    carassiuscarassius
    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?

    #1308
    Anh TranAnh Tran
    Keymaster

    Hi carassius,

    Yes, you're right about using Conditional Logic extension. It's made exactly for this purpose. Please try it and let us know if it works for you.

    Thanks.

    #1309
    carassiuscarassius
    Participant

    Ok, will try and see if I can get it to work and come back if I have any questions 🙂

    Thanks
    Jon

    #1310
    carassiuscarassius
    Participant

    Cant seem to get my head around how I can use the conditional logic.

    I can see how it makes different elements visible but that is now what I am after (unless I am thinking this wrong?)

    I want to look at the post selector

    $name = rwmb_meta( 'p52_quote_name', 'type=text' );

    and then take that and return the associated phone number to that contact
    $phone = rwmb_meta( 'p52_contact_phone', 'type=text' );

    So I could have something sort of like this (which is totally incorrect, I know)

    <?php

    function p52_register_quote_meta_boxes( $meta_boxes ) {

    $prefix = 'p52_quote_';
    $meta_boxes[] = array(
    'id' => $prefix . 'customer',
    'title' => 'Customer Info',
    'pages' => 'p52-quote',
    'context' => 'normal',
    'priority' => 'low',

    'fields' => array(

    array(
    'name' => 'Name',
    'id' => $prefix . 'name',
    'type' => 'post',
    'post_type' => 'p52-contact',
    'placeholder' => 'Select a Customer',
    'columns' => 6
    ),

    array(
    'name' => 'Phone',
    'id' => $prefix . 'phone',
    'post_type' => 'p52-contact',
    'std' => get_post_meta($post->ID, 'p52_quote_phone', true),
    'type' => 'text',
    'columns' => 6
    ),
    return $meta_boxes;
    }

    add_filter( 'rwmb_meta_boxes', 'p52_register_quote_meta_boxes' );

    #1311
    carassiuscarassius
    Participant

    Warning: Invalid argument supplied for foreach() in C:\wamp\www\wordpress\wp-content\plugins\project52\plugins\meta-box-conditional-logic\inc\class-conditional-logic.php on line81

    #1320
    Tan NguyenTan Nguyen
    Participant

    Hi carassius,

    Thanks for using Conditional Logic

    As your requested, I've created a snippet with comments so you can see and use in your project. Basically, I use a 'post' field to display all customers and create a phone field which store customer phone and a set of phone fields equal to num of customers which has default value inside, when user post data, the visible phone field will replace default phone field. Have fun 🙂

    https://pastebin.com/6qC7CJwh

    #1324
    carassiuscarassius
    Participant

    I get an error installing conditional logic

    #1325
    Tan NguyenTan Nguyen
    Participant

    Can you show me that error?

    #1326
    carassiuscarassius
    Participant

    Warning: Invalid argument supplied for foreach() in C:\wamp\www\wordpress\wp-content\plugins\project52\plugins\meta-box-conditional-logic\inc\class-conditional-logic.php on line81

    this is how i am installing it
    if( !class_exists( 'MB_Conditional_Logic' ) ) {
    include( plugin_dir_path( __FILE__ ) . '/plugins/meta-box-conditional-logic/meta-box-conditional-logic.php');
    }

    #1327
    carassiuscarassius
    Participant

    also, if I do a var_dump ($customers) I dont get any of the metabox fields, so I must have something wrong with loading them into the loop when creating my cpt

    #1328
    carassiuscarassius
    Participant

    Just redownloaded my core plugins and added that version, and the error is now gone, maybe a corrupt download

    #1329
    Tan NguyenTan 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
    carassiuscarassius
    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 NguyenTan 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
    carassiuscarassius
    Participant

    I keep getting that foreach error on line 81

    https://pastebin.com/Wr6E0kmA

Viewing 15 posts - 1 through 15 (of 43 total)
  • The topic ‘Display element based on 'post'’ is closed to new replies.