Creating Relationships
-
Hello, I am trying to create a relationship between two dropdown menus. The easiest way for me to describe this is to use car manufacturers as an example. If I have one dropdown with Ford and Chevy as the choices I would like the second dropdown to populate with their related models.
I watched this video (Grow Beyond Posts & Pages: Introduction to Pods Framework) and I thought I was on the right track but I cannot seem to figure out how to display the related models. It only displays all the models. The video seemed to focus on displaying the relationships on the front end but I want to display them on the backend. Any direction would be great.
-
The values of Pods fields only get saved when the post gets saved.
So for the values of one field to be filtered based on the values of another field, it requires a save and refresh.
For this reason, it’s not recommended to attempt this type of data structure with Pods.
The recommended approach would be a custom meta field with an AJAX call using JavaScript to allow the values for one field to change based on the value of another field.
Here’s one simple workaround that still uses Pods:
- Create a hierarchical taxonomy, Make & Model, where parent terms are the Make, and child terms are the Model.
- Add a field to a custom post type, in this example I’ve created
Service Appointments, with a relationship field related to the taxonomy. - Add custom JavaScript to disable selecting Makes, so only Models can be selected. Makes can be derived from the parent of the selected model.
Here is a Pods package export for this setup:
{ "@meta": { "version": "2.9.10.1", "build": 1672139259 }, "settings": { "types_only": "0", "watch_changed_fields": "0", "metadata_integration": "1", "metadata_override_get": "0", "session_auto_start": "0", "wisdom_opt_out": "1" }, "pods": [ { "name": "make_model", "id": 6, "label": "Make & Model", "description": "", "type": "taxonomy", "storage": "meta", "label_singular": "Make & Model", "public": "1", "show_ui": "1", "hierarchical": "1", "rest_enable": "1", "_migrated_28": "1", "show_in_menu": "1", "menu_location": "objects", "menu_position": "30", "show_in_nav_menus": "1", "show_tagcloud": "1", "show_in_quick_edit": "1", "show_admin_column": "0", "built_in_post_types_service_appointment": "0", "menu_icon": "dashicons-car", "groups": [ { "name": "more_fields", "id": 7, "label": "More Fields", "description": "", "weight": 0, "fields": [] } ] }, { "name": "service_appointment", "id": 10, "label": "Service Appointments", "description": "", "type": "post_type", "storage": "meta", "label_singular": "Service Appointment", "public": "1", "show_ui": "1", "rest_enable": "1", "supports_title": "1", "supports_editor": "1", "_migrated_28": "1", "required": "0", "built_in_taxonomies_make": "1", "built_in_taxonomies_model": "1", "built_in_taxonomies_make_model": "0", "groups": [ { "name": "more_fields", "id": 11, "label": "More Fields", "description": "", "weight": 0, "fields": [ { "name": "make_and_model", "id": 61, "label": "Make & Model", "description": "", "weight": 0, "type": "pick", "pick_object": "taxonomy", "pick_val": "make_model", "sister_id": "-- Select One --", "pick_table": "-- Select One --", "required": "0", "pick_format_type": "single", "pick_format_single": "dropdown", "pick_format_multi": "list", "pick_display_format_multi": "default", "pick_display_format_separator": ", ", "pick_allow_add_new": "1", "pick_taggable": "0", "pick_show_icon": "1", "pick_show_edit_link": "1", "pick_show_view_link": "1", "pick_limit": "0", "pick_user_role": "Administrator", "pick_post_status": "publish", "pick_post_author": "0", "repeatable": "0", "repeatable_format": "default", "roles_allowed": "administrator", "rest_pick_response": "array", "rest_pick_depth": "1" } ] } ] } ] }And here is JavaScript for disabling selecting Makes:
<?php // https://developer.ww.wp.xz.cn/reference/hooks/admin_footer-hook_suffix/ add_action( 'admin_footer-edit.php', 'edit_make_model_js', 1000 ); add_action( 'admin_footer-post-new.php', 'edit_make_model_js', 1000 ); function edit_make_model_js() { if ( 'service_appointment' !== get_post_type() ) { return; } ?> <script> jQuery( document ).ready( function( $ ){ $('#pods-form-ui-pods-meta-make-and-model option').each( function(){ if ( $( this ).text().indexOf(' ') === -1 && $( this ).text().indexOf('--') === -1 ) { $(this).attr( 'disabled', 'disabled' ); } }); } ); </script> <?php }Here is a plugin which, if installed for example to
wp-content/plugins/make-model/make-model.php, will add a relational two-select box for managing a hierarchical taxonomy,make_model:<?php /** * Plugin Name: Make & Model * Description: Register a taxonomy <code>make_model</code> and add a meta box for managing it to post type <code>service_appointment</code>. * Version: 1 * Author: 𝜑δ•ℂᴹ * Author URI: https://pd.cm/ * Plugin URI: https://ww.wp.xz.cn/support/topic/creating-relationships/ */ add_action( 'init', function(){ // https://developer.ww.wp.xz.cn/reference/functions/register_taxonomy/ register_taxonomy( 'make_model', [ 'service_appointment' ], [ 'show_ui' => true, 'show_in_quick_edit' => true, 'meta_box_cb' => false, 'hierarchical' => true, 'labels' => [ 'name' => _x( 'Makes & Models', 'taxonomy general name', 'make_model' ), 'singular_name' => _x( 'Make & Model', 'taxonomy singular name', 'make_model' ), 'search_items' => __( 'Search Makes & Models', 'make_model' ), 'all_items' => __( 'All Makes & Models', 'make_model' ), 'parent_item' => __( 'Parent Make & Model', 'make_model' ), 'parent_item_colon' => __( 'Parent Make & Model:', 'make_model' ), 'edit_item' => __( 'Edit Make & Model', 'make_model' ), 'update_item' => __( 'Update Make & Model', 'make_model' ), 'add_new_item' => __( 'Add New Make & Model', 'make_model' ), 'new_item_name' => __( 'New Make & Model Name', 'make_model' ), 'menu_name' => __( 'Make & Model', 'make_model' ), ], ] ); } ); add_action( 'admin_init', function(){ add_meta_box( 'make_model', __( 'Make & Model', 'make_model' ), function(){ // https://developer.ww.wp.xz.cn/reference/classes/wp_term_query/__construct/ $terms = get_terms([ 'taxonomy' => 'make_model', 'orderby' => 'parent', 'hide_empty' => false, 'fields' => 'all', ]); $selected_terms = wp_get_object_terms( get_the_ID(), 'make_model', [ 'fields' => 'ids', ] ); $make_models = []; foreach( $terms as $term ) { if ( $term->parent === 0 ) { $make_models[ $term->term_id ]['name'] = $term->name; $make_models[ $term->term_id ]['selected'] = in_array( $term->term_id, $selected_terms ); }else { $make_models[ $term->parent ]['models'][ $term->term_id ]['name'] = $term->name; $make_models[ $term->parent ]['models'][ $term->term_id ]['selected'] = in_array( $term->term_id, $selected_terms ); } } ?> <select id="make" name="make"> <?php foreach( $make_models as $make_id => $make ) { printf( '<option value="%d" data-models="%s" %s>%s</option>', esc_attr( $make_id ), esc_attr( json_encode( $make['models'] ) ), selected( $make['selected'] ), esc_html( $make['name'] ) ); } ?> </select> <select id="model" name="model"></select> <script> jQuery( document ).ready( function($){ $('#make').on( 'change', function(){ let models = JSON.parse( $(this).find('option:selected').attr( 'data-models' ) ); $('#model').find('option').remove(); $.each( models, function( term_id, model_obj ){ let $option = $('<option>').val( term_id ).text( model_obj.name ); if ( model_obj.selected ) { $option.attr( 'selected', 'selected' ); } $('#model').append( $option ); }); }); $('#make').trigger('change'); }); </script> <?php }, [ 'service_appointment' ], 'advanced', 'high' ); } ); add_action( 'save_post', function( $post_id ){ if ( 'service_appointment' !== get_post_type( $post_id ) ) { return; } if ( ! empty( $_POST['make'] ) && ! empty( $_POST['model'] ) ) { wp_set_object_terms( $post_id, [ (int) $_POST['make'], (int) $_POST['model'] ], 'make_model', false ); } } );The plugin causes the selection of “Make” to change the available options for “Model”, and causes the selection to override taxonomy term assignments for the post type. So the usual taxonomy term selection box is disabled and replaced with the dropdown, but other taxonomy user interface is still available:


Pods is still used for registering the custom post type, here is the Pods config for that:
{
"@meta": {
"version": "2.9.10.1",
"build": 1672143231
},
"pods": [
{
"name": "service_appointment",
"id": 10,
"label": "Service Appointments",
"description": "",
"type": "post_type",
"storage": "meta",
"label_singular": "Service Appointment",
"public": "1",
"show_ui": "1",
"rest_enable": "1",
"supports_title": "1",
"supports_editor": "1",
"_migrated_28": "1",
"required": "0",
"built_in_taxonomies_make": "1",
"built_in_taxonomies_model": "1",
"built_in_taxonomies_make_model": "1",
"groups": [
{
"name": "more_fields",
"id": 11,
"label": "More Fields",
"description": "",
"weight": 0,
"fields": []
}
]
}
]
}register_post_type() could also be used, in which case Pods wouldn’t be necessary for the field to work.
-
This reply was modified 3 years, 5 months ago by
pdclark.
Thanks Paul for a lot of detailed information. I am not home yet but will try out your suggestions later tonight.
The topic ‘Creating Relationships’ is closed to new replies.