Support

Account

Home Forums Front-end Issues Select2 not pulling saved values on MultiSite Reply To: Select2 not pulling saved values on MultiSite

  • Thanks John,

    I’m defining the fields in the php, which I will include below. The taxonomy list is generated from a taxonomy defined on the root site. On subsites, I use the query and results hooks to switch to the root site for the query then back to the originating site after the query. I can also include the php and js hooks I’m using. The fields in question are ‘peopletype_taxonomy_list` which filters ‘people_by_type’.

    		acf_add_local_field_group(array(
    			'key' => 'group_59d53ce54567f',
    			'title' => 'People Group',
    			'fields' => array(
    				array(
    					'key' => 'field_59dd3068b24e6',
    					'label' => 'Copy',
    					'name' => 'copy',
    					'type' => 'wysiwyg',
    					'instructions' => '',
    					'required' => 0,
    					'conditional_logic' => 0,
    					'wrapper' => array(
    						'width' => '',
    						'class' => '',
    						'id' => '',
    					),
    					'default_value' => '',
    					'tabs' => 'all',
    					'toolbar' => 'full',
    					'media_upload' => 1,
    					'delay' => 0,
    				),
    				array(
    					'key' => 'field_5c27f7a668daf',
    					'label' => 'Add By Individual',
    					'name' => 'add_by_individual',
    					'type' => 'true_false',
    					'instructions' => '',
    					'required' => 0,
    					'conditional_logic' => 0,
    					'wrapper' => array(
    						'width' => '',
    						'class' => '',
    						'id' => '',
    					),
    					'message' => '',
    					'default_value' => 0,
    					'ui' => 1,
    					'ui_on_text' => '',
    					'ui_off_text' => '',
    				),
    				array(
    					'key' => 'field_5c27f7d868db0',
    					'label' => 'Add By People Type',
    					'name' => 'add_by_people_type',
    					'type' => 'true_false',
    					'instructions' => '',
    					'required' => 0,
    					'conditional_logic' => 0,
    					'wrapper' => array(
    						'width' => '',
    						'class' => '',
    						'id' => '',
    					),
    					'message' => '',
    					'default_value' => 0,
    					'ui' => 1,
    					'ui_on_text' => '',
    					'ui_off_text' => '',
    				),
    				array(
    					'key' => 'field_5c27f7eb68db1',
    					'label' => 'Add By Site',
    					'name' => 'add_by_site',
    					'type' => 'true_false',
    					'instructions' => '',
    					'required' => 0,
    					'conditional_logic' => 0,
    					'wrapper' => array(
    						'width' => '',
    						'class' => '',
    						'id' => '',
    					),
    					'message' => '',
    					'default_value' => 0,
    					'ui' => 1,
    					'ui_on_text' => '',
    					'ui_off_text' => '',
    				),
    				array(
    					'key' => 'field_59d53cee6770e',
    					'label' => 'People as Individuals',
    					'name' => 'people_as_individuals',
    					'type' => 'repeater',
    					'instructions' => '',
    					'required' => 0,
    					'conditional_logic' => array(
    						array(
    							array(
    								'field' => 'field_5c27f7a668daf',
    								'operator' => '==',
    								'value' => '1',
    							),
    						),
    					),
    					'wrapper' => array(
    						'width' => '',
    						'class' => '',
    						'id' => '',
    					),
    					'collapsed' => '',
    					'min' => 0,
    					'max' => 0,
    					'layout' => 'table',
    					'button_label' => '',
    					'sub_fields' => array(
    						array(
    							'key' => 'field_59d53d266770f',
    							'label' => 'Person',
    							'name' => 'person',
    							'type' => 'post_object',
    							'instructions' => '',
    							'required' => 0,
    							'conditional_logic' => 0,
    							'wrapper' => array(
    								'width' => '',
    								'class' => '',
    								'id' => '',
    							),
    							'post_type' => array(
    								0 => 'furman-person',
    							),
    							'taxonomy' => '',
    							'allow_null' => 0,
    							'multiple' => 0,
    							'return_format' => 'object',
    							'ui' => 1,
    						),
    					),
    				),
    				array(
    					'key' => 'field_5c2d0d1860d0f',
    					'label' => 'Select People Type',
    					'name' => 'peopletype_taxonomy_list',
    					'type' => 'taxonomy',
    					'instructions' => '',
    					'required' => 0,
    					'conditional_logic' => array(
    						array(
    							array(
    								'field' => 'field_5c27f7d868db0',
    								'operator' => '==',
    								'value' => '1',
    							),
    						),
    					),
    					'wrapper' => array(
    						'width' => '',
    						'class' => '',
    						'id' => '',
    					),
    					'taxonomy' => 'furman-person-type',
    					'field_type' => 'select',
    					'allow_null' => 0,
    					'add_term' => 1,
    					'save_terms' => 0,
    					'load_terms' => 0,
    					'return_format' => 'id',
    					'multiple' => 0,
    				),
    				array(
    					'key' => 'field_5c27fa0480c21',
    					'label' => 'People by Type',
    					'name' => 'people_by_type',
    					'type' => 'repeater',
    					'instructions' => '',
    					'required' => 0,
    					'conditional_logic' => array(
    						array(
    							array(
    								'field' => 'field_5c27f7d868db0',
    								'operator' => '==',
    								'value' => '1',
    							),
    							array(
    								'field' => 'field_5c2d0d1860d0f',
    								'operator' => '!=empty',
    							),
    						),
    					),
    					'wrapper' => array(
    						'width' => '',
    						'class' => '',
    						'id' => '',
    					),
    					'collapsed' => '',
    					'min' => 0,
    					'max' => 0,
    					'layout' => 'table',
    					'button_label' => '',
    					'sub_fields' => array(
    						array(
    							'key' => 'field_5c27fa0480c22',
    							'label' => 'Person',
    							'name' => 'person',
    							'type' => 'post_object',
    							'instructions' => '',
    							'required' => 0,
    							'conditional_logic' => 0,
    							'wrapper' => array(
    								'width' => '',
    								'class' => '',
    								'id' => '',
    							),
    							'post_type' => '',
    							'taxonomy' => '',
    							'allow_null' => 0,
    							'multiple' => 0,
    							'return_format' => 'object',
    							'ui' => 1,
    						),
    					),
    				),
    				array(
    					'key' => 'field_5c2d0d734763e',
    					'label' => 'Select Site',
    					'name' => 'site_taxonomy_list',
    					'type' => 'taxonomy',
    					'instructions' => '',
    					'required' => 0,
    					'conditional_logic' => array(
    						array(
    							array(
    								'field' => 'field_5c27f7eb68db1',
    								'operator' => '==',
    								'value' => '1',
    							),
    						),
    					),
    					'wrapper' => array(
    						'width' => '',
    						'class' => '',
    						'id' => '',
    					),
    					'taxonomy' => 'furman-site',
    					'field_type' => 'select',
    					'allow_null' => 0,
    					'add_term' => 1,
    					'save_terms' => 0,
    					'load_terms' => 0,
    					'return_format' => 'id',
    					'multiple' => 0,
    				),
    				array(
    					'key' => 'field_5c28136b1102f',
    					'label' => 'People by Site',
    					'name' => 'people_by_site',
    					'type' => 'repeater',
    					'instructions' => '',
    					'required' => 0,
    					'conditional_logic' => array(
    						array(
    							array(
    								'field' => 'field_5c27f7eb68db1',
    								'operator' => '==',
    								'value' => '1',
    							),
    							array(
    								'field' => 'field_5c2d0d734763e',
    								'operator' => '!=empty',
    							),
    						),
    					),
    					'wrapper' => array(
    						'width' => '',
    						'class' => '',
    						'id' => '',
    					),
    					'collapsed' => '',
    					'min' => 0,
    					'max' => 0,
    					'layout' => 'table',
    					'button_label' => '',
    					'sub_fields' => array(
    						array(
    							'key' => 'field_5c28136b11030',
    							'label' => 'Person',
    							'name' => 'person',
    							'type' => 'post_object',
    							'instructions' => '',
    							'required' => 0,
    							'conditional_logic' => 0,
    							'wrapper' => array(
    								'width' => '',
    								'class' => '',
    								'id' => '',
    							),
    							'post_type' => '',
    							'taxonomy' => '',
    							'allow_null' => 0,
    							'multiple' => 0,
    							'return_format' => 'object',
    							'ui' => 1,
    						),
    					),
    				),
    			),
    			'location' => array(
    				array(
    					array(
    						'param' => 'post_type',
    						'operator' => '==',
    						'value' => 'post',
    					),
    				),
    			),
    			'menu_order' => 0,
    			'position' => 'normal',
    			'style' => 'default',
    			'label_placement' => 'top',
    			'instruction_placement' => 'label',
    			'hide_on_screen' => '',
    			'active' => 1,
    			'description' => '',
    		));

    Here are the hooks being used.

    /*
     * Action hooks to modify query arguments within the People Group module
     */
    
    /*
     * Sets query args for Individual People Field
     */
    function individualPeopleQuery( $args, $field, $post_id ) {
    	$args['post_type'] = \Furman\PostTypes\People::POST_TYPE;
    
    	return $args;
    
    }
    
    add_filter('acf/fields/post_object/query/key=field_59d53d266770f', 'individualPeopleQuery', 10, 3);
    
    /*
     * Ensures that the title is correct when querying from child sites
     */
    function individualPeopleResult( $title, $post ) {
    	if ( strpos( $title, 'no title' ) ) {
    		$title = $post->post_title;
    	}
    
    	return $title;
    }
    
    add_filter('acf/fields/post_object/result/key=field_59d53d266770f', 'individualPeopleResult', 10, 3);
    
    function peopleTypeTaxonomyQuery( $args, $field, $post_id ) {
    	if ( ! Site::isRoot() ) {
    		$currentSite = get_current_blog_id();
    		switch_to_blog( Site::getRootBlogId() );
    		set_transient( 'peopleTypeTaxonomyQuery', $currentSite );
    	}
    
    	return $args;
    }
    
    add_filter('acf/fields/taxonomy/query/key=field_5c2d0d1860d0f', 'peopleTypeTaxonomyQuery', 10, 3);
    
    function peopleTypeTaxonomyResult( $title ) {
    	$originatingSite = get_transient( 'peopleTypeTaxonomyQuery' );
    	if ( Site::isRoot() && $originatingSite ) {
    		delete_transient( 'peopleTypeTaxonomyQuery' );
    		switch_to_blog( $originatingSite );
    	}
    
    	return $title;
    }
    
    add_filter('acf/fields/taxonomy/result/key=field_5c2d0d1860d0f', 'peopleTypeTaxonomyResult', 10, 3);
    
    //add_filter('acf/load_field/key=field_5c2d0d1860d0f', 'peopleType');
    
    /*
     * Set up custom query args received from AJAX requests.
     */
    function peopleByTypeQuery( $args, $field, $post_id ) {
    
    	if ( ! Site::isRoot() ) {
    		$currentSite = get_current_blog_id();
    		switch_to_blog( Site::getRootBlogId() );
    		set_transient( 'peopleByTypeQuery', $currentSite );
    	}
    
    	$args['post_type'] = \Furman\PostTypes\People::POST_TYPE;
    
    	if ( isset( $_POST['query-args'] ) ) {
    		$termId = $_POST['query-args']['term_id'];
    		$args['tax_query'] = array(
    			array(
    				'taxonomy' => \Furman\Taxonomies\PeopleTypes::TAXONOMY,
    				'field' => 'term_id',
    				'terms' => $termId
    			)
    		);
    	}
    
    	return $args;
    
    }
    
    add_filter('acf/fields/post_object/query/key=field_5c27fa0480c22', 'peopleByTypeQuery', 10, 3);
    
    function peopleByTypeResult($title) {
    	$originatingSite = get_transient( 'peopleByTypeQuery' );
    	if ( Site::isRoot() && $originatingSite ) {
    		delete_transient( 'peopleByTypeQuery' );
    		switch_to_blog( $originatingSite );
    	}
    
    	return $title;
    }
    
    add_filter('acf/fields/post_object/result/key=field_5c27fa0480c22', 'peopleByTypeResult', 10, 3);
    
    function peopleSiteTaxonomyQuery( $args, $field, $post_id ) {
    	if ( ! Site::isRoot() ) {
    		$currentSite = get_current_blog_id();
    		switch_to_blog( Site::getRootBlogId() );
    		set_transient( 'peopleSiteTaxonomyQuery', $currentSite );
    	}
    
    	return $args;
    }
    
    add_filter('acf/fields/taxonomy/query/key=field_5c2d0d734763e', 'peopleSiteTaxonomyQuery', 10, 3);
    
    function peopleSiteTaxonomyResult( $title ) {
    	$originatingSite = get_transient( 'peopleSiteTaxonomyQuery' );
    	if ( Site::isRoot() && $originatingSite ) {
    		delete_transient( 'peopleSiteTaxonomyQuery' );
    		switch_to_blog( $originatingSite );
    	}
    
    	return $title;
    }
    
    add_filter('acf/fields/taxonomy/result/key=field_5c2d0d734763e', 'peopleSiteTaxonomyResult', 10, 3);
    
    function peopleBySiteQuery( $args, $field, $post_id ) {
    
    	if ( ! Site::isRoot() ) {
    		$currentSite = get_current_blog_id();
    		switch_to_blog( Site::getRootBlogId() );
    		set_transient( 'peopleBySiteQuery', $currentSite );
    	}
    
    	$args['post_type'] = \Furman\PostTypes\People::POST_TYPE;
    
    	if ( isset( $_POST['query-args'] ) ) {
    		$termId = $_POST['query-args']['term_id'];
    		$args['tax_query'] = array(
    			array(
    				'taxonomy' => \Furman\Taxonomies\Sites::TAXONOMY,
    				'field' => 'term_id',
    				'terms' => $termId
    			)
    		);
    	}
    
    	return $args;
    
    }
    
    add_filter('acf/fields/post_object/query/key=field_5c28136b11030', 'peopleBySiteQuery', 10, 3);
    
    function peopleBySiteResult($title) {
    	$originatingSite = get_transient( 'peopleBySiteQuery' );
    	if ( Site::isRoot() && $originatingSite ) {
    		delete_transient( 'peopleBySiteQuery' );
    		switch_to_blog( $originatingSite );
    	}
    
    	return $title;
    }
    
    add_filter('acf/fields/post_object/result/key=field_5c27fa0480c22', 'peopleBySiteResult', 10, 3);

    Here are the JS API hooks in use

    document.addEventListener('DOMContentLoaded', () => {
    
        acf.add_action('select2_init', function( $select, args, settings, field ){
            console.log('SELECT2_SELECT:', $select);
            console.log('SELECT2_ARGS:', args);
            console.log('SELECT2_SETTINGS:', settings);
            console.log('SELECT2_FIELD:', field);
            // $select (jQuery) select element
            // args (object) args given to the select2 function
            // settings (object) settings given to the acf.select2 function
            // field (object) field instance
        });
        /*
        Uses the ACF JS API.  These filters allow the post object fields to be filtered based on a separate acf taxonomy field.
         */
        acf.add_filter('select2_ajax_data', function (data, args, $input, field, instance) {
            //People Type filter
            if (field[0].dataset.key === 'field_5c27fa0480c22') {
                let taxonomySelect = $('.acf-field-5c2d0d1860d0f').find('.select2-hidden-accessible').select2('data');
                data['query-args'] = {
                    'taxonomy': 'furman-person-type',
                    'term_id': taxonomySelect[0].id,
                    'term_title': taxonomySelect[0].text
                };
            }
            //Site filter
            if (field[0].dataset.key === 'field_5c28136b11030') {
                let taxonomySelect = $('.acf-field-5c2d0d734763e').find('.select2-hidden-accessible').select2('data');
                data['query-args'] = {
                    'taxonomy': 'furman-site',
                    'term_id': taxonomySelect[0].id,
                    'term_title': taxonomySelect[0].text
                };
            }
    
            // return
            return data;
    
        });
    
    });