Display element based on 'post'
- This topic has 42 replies, 3 voices, and was last updated 9 years, 8 months ago by
carassius.
-
AuthorPosts
-
August 27, 2015 at 9:15 AM #1306
carassius
ParticipantIf I create a basic customer cpt
name
phone
email
addressand 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?
August 28, 2015 at 8:26 AM #1308Anh Tran
KeymasterHi 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.
August 28, 2015 at 8:32 AM #1309carassius
ParticipantOk, will try and see if I can get it to work and come back if I have any questions 🙂
Thanks
JonAugust 28, 2015 at 8:52 AM #1310carassius
ParticipantCant 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' );
August 28, 2015 at 9:05 AM #1311carassius
ParticipantWarning: 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
August 29, 2015 at 2:18 AM #1320Tan Nguyen
ParticipantHi 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 🙂
August 29, 2015 at 9:03 AM #1324carassius
ParticipantI get an error installing conditional logic
August 29, 2015 at 9:21 AM #1325Tan Nguyen
ParticipantCan you show me that error?
August 29, 2015 at 9:22 AM #1326carassius
ParticipantWarning: 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');
}August 29, 2015 at 9:23 AM #1327carassius
Participantalso, 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
August 29, 2015 at 9:38 AM #1328carassius
ParticipantJust redownloaded my core plugins and added that version, and the error is now gone, maybe a corrupt download
August 29, 2015 at 1:44 PM #1329Tan Nguyen
ParticipantOk 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'?
August 30, 2015 at 7:49 PM #1335carassius
ParticipantThanks 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] ); } } );
August 31, 2015 at 10:37 AM #1344Tan Nguyen
ParticipantHi 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 ispost
andp52_quote_phone
which ishidden
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 instd
is: 0123456789This snippet wich you've asked is to remove all faked text fields defined above, and store visible field (
p52_quote_phone_1
) top52_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?
August 31, 2015 at 11:13 AM #1345carassius
ParticipantI keep getting that foreach error on line 81
-
AuthorPosts
- The topic ‘Display element based on 'post'’ is closed to new replies.