Linking Two Custom Post Types in select/dropdown field

Support Meta Box AIO Linking Two Custom Post Types in select/dropdown field

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #41942
    collinsonfamily@gmail.com[email protected]
    Participant

    Hello,

    I'm new to Meta Box so forgive my naivity. I'm trying to create a page that allows the user to enter exam details, such as the start time, date, end time, date and exam name. I also want an exam location field to show in a dropdown or select box, that is populated by the contents of another custom post type called Exam locations, which has a custom field called Excam location.

    How do I get the dropdown/select field to populate from the exam location custom field in another post type?

    #41946
    PeterPeter
    Moderator

    Hello,

    Meta Box supports a field post that helps you select posts from another post type to save as a post meta. Please read more in the documentation https://docs.metabox.io/fields/post/

    #43415
    leebazCDSleebazCDS
    Participant

    hi, the 'post' field is great! Is it possible to then select a saved field from within the post type.

    Example:
    1st Custom post type: Companies, has Custom Fields for contacts Name, email, Phone of various company contacts.
    Customer 1: Nike
    Contacts: Bill, Susan, Jimmy, Jane
    Customer 2: Adidas
    Contacts: Adrian, Khaled

    2nd Custom post type: Orders, which has custom field dropdown for 'Customer' using from the 'post' select choosing from Customers, then which contact details for that Customer? I want to select A number of the contacts so that I can then display which contacts are relevant for that Order.

    Hope that makes sense!
    Thanks!

    #43419
    PeterPeter
    Moderator

    Hello Lee,

    In the frontend, you can create custom code to show which Customer is associated with the contact and show which Order has that customer field value. There should be 2 nested loops in this case.

    #43420
    leebazCDSleebazCDS
    Participant

    Hi Peter,

    Thanks! My query isn't regarding the front-end output, but rather when inputing data into the custom fields. I used Customer and Order as examples, but I'll refer to my exact use case so as to be more clear:

    We install MET masts for weather monitoring. So I have a custom post type 'masts' and custom fields for the mast properties, such as location, height, data of installation etc.

    We have various customers and each customer may have one or more contact for a particular mast. So I have a custom post type 'Customer' and custom fields for the customer including the various contacts.

    When creating a mast I populate the 'Customer' field using the 'post' field type.

    Then for the customer contacts, I want to make a selectable list of the contacts particular to the selected customer.

    Is that possible within the field builder plug in, or would I need a custom function elsewhere?

    #43422
    PeterPeter
    Moderator

    Hello,

    It isn't possible with the builder. You can follow the documentation below to create a custom field type (select field) for the "selectable list of the contacts particular to the selected customer".
    https://docs.metabox.io/creating-new-field-types/

    #43437
    leebazCDSleebazCDS
    Participant

    Thanks! I'm trying to register my own field type class to see if I can get this to work.
    I have a file called customer-field-type.php in my /inc/ folder.

    I have put the following into functions.php:

    add_action( 'init', 'prefix_load_phone_type' );
    
    function prefix_load_phone_type() {
    	$meta_boxes_extend= get_template_directory() . '/inc/customer-field-type.php';
    	if ( is_readable( $meta_boxes_extend ) ) {
    		require  $meta_boxes_extend;
    	}
    }

    In my customer-field-type.php file is the code taken from your example page:

    class RWMB_Phone_Field extends RWMB_Field {
        public static function html( $meta, $field ) {
            return sprintf(
                '<input type="text" name="%s" id="%s" class="rwmb-phone" value="%s" pattern="\d{3}-\d{3}-\d{4}">',
                $field['field_name'],
                $field['id'],
                $meta
            );
        }
    }

    however, when I go to make a new field in the builder I can't find this new field in the list.
    What am I doing wrong?

    #43442
    leebazCDSleebazCDS
    Participant

    This is my attempt to create a select field from custom post type, custom field, but it's not appearing in the builder yet...

    Same file structure as my previous post:

    
    <?php
    
    add_filter( 'rwmb_field_types', 'register_custom_field_type' );
    
    function register_custom_field_type( $types ) {
        $types['select_from_sw_customers'] = 'SW_Customers_Select_Field';
    
        return $types;
    }
    
    class SW_Customers_Select_Field extends RWMB_Field {
        
        public static function html( $meta, $field ) {
            // Args for the WP_Query
            $args = array(
                'post_type'      => 'sw_customers',
                'posts_per_page' => -1,
                'post_status'    => 'publish'
            );
    
            $query = new WP_Query( $args );
            $options = "";
    
            if ( $query->have_posts() ) {
                while ( $query->have_posts() ) {
                    $query->the_post();
    
                    // Get the custom field value from sw_customers
                    $groups = get_post_meta( get_the_ID(), 'contact_details', true );
                        foreach ( $groups as $group ) {
                            $customer_name = $group[ 'name' ] ?? '';
                        }
    
                    if ( ! empty( $customer_name ) ) {
                        $selected = selected( $meta, get_the_ID(), false );
                        $options .= "<option value='" . esc_attr( get_the_ID() ) . "' {$selected}>" . esc_html( $customer_name ) . "</option>";
                    }
                }
                wp_reset_postdata();
            }
    
            return sprintf(
                '<select name="%s" id="%s">%s</select>',
                $field['field_name'],
                $field['id'],
                $options
            );
        }
    }
    
    
    #43450
    PeterPeter
    Moderator

    Hello,

    If you want to create a custom field in the builder, please follow the documentation https://docs.metabox.io/extensions/meta-box-builder/#adding-your-own-field-types

    Note: we do not support the custom code for your specific needs. We support a customization service with an extra fee. Please contact us here for more information https://metabox.io/contact/

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.