Support

Account

Forum Replies Created

  • John, I wanted to come back once again and thank you for your help. The custom field solution works well and will be a great tool for future endeavors I’m sure. Thanks for all the time you put in helping me get over the hump here.

  • I don’t disagree with you, and if this were my own client I’d have gone that route a long time ago. However, I’m an agency developer and I’m not the lead on this one. I can push back on my lead and communicate the difficulties, but I have a feeling it’s going to be the select2 or bust. It’s for a big university site, and this particular taxonomy will have many terms so the search will come in handy.

  • I am using a taxonomy field. I didn’t try that because the docs don’t say you can use key= on render_Field, it just shows type=. I tried that and it does fire correctly but still doesn’t fix the issue. Switching to the root site (and not switching back) allows the taxonomy field to work as expected, but once I add the later hook to switch back it goes back to not loading the field value.

  • I just saw your most recent reply. I thought of using a basic select field and using load_field to setup the choices, but the client wants the fancy select2 field with the searchbar and ajax functionality, so I’m going to try to create my own field based on the taxonomy select2 field and fall back to the basic select if that’s not possible. If I can use the taxonomy select code as a template and just modify the term query I think I should be able to make it work.

  • I finally tracked down the issue and thought I’d post my findings. The hack option didn’t work. I’ll post the code I used and you can tell me if that’s what you were suggesting I try. So far as I can tell, the breakdown happens on line 643 of class-acf-field-taxonomy.php in the render_field_select function. $this->get_terms doesn’t return any terms on subsites. It looks like i’ll have to pursue creating my own field that modifies the taxonomy field.

    function switchBeforeRender( $field ) {
    
    	if ( ! Site::isRoot() ) {
    		$currentSite = get_current_blog_id();
    		switch_to_blog( Site::getRootBlogId() );
    		set_transient( 'switchBeforeRender', $currentSite );
    	}
    
    }
    add_action( 'acf/render_field/type=select', 'switchBeforeRender', 8, 1 );
    
    function switchAfterRender( $field ) {
    
    	$originatingSite = get_transient( 'switchBeforeRender' );
    	$currentSite     = get_current_blog_id();
    	if ( $currentSite === 1 && $originatingSite ) {
    		delete_transient( 'switchBeforeRender' );
    		switch_to_blog( $originatingSite );
    	}
    
    }
    add_action( 'acf/render_field/type=select', 'switchAfterRender', 15, 1 );
    
  • John, thanks so much for all of your insight. I’ll look into these options.

  • Thanks John that helps. I’m trying to override whatever code is preventing my taxonomy value from loading on subsites. To your knowledge, is prepare_field the closest thing to render that you can hook into? I’ve tried messing with that one and adjusting the priority higher and higher. However, the field value is present and correct no matter what I do. It’s super frustrating. The field value is there but the select option is never rendered.

  • Hi John,

    Sorry I hit the problem solved button on accident. As a side question, do you know how the data for select2 fields is built and placed on the window object in the js? The jQuery objects are there with the data on page load but I don’t see any xhr data in devtools indicating an ajax request was made to populate those objects. So now my question is, how in the heck did the data get there?

  • Here’s some more info I’ve found. So to my knowledge the php hook that occurs right before field render is acf/prepare_field. Dumping the field shows that the value is present. Now the big question is, why isn’t it making it into the select2 jQuery object, and what handles the creation of that piece?

    array (size=31)
      'ID' => int 0
      'key' => string 'field_59e61ff30ff19_field_5c2d0d1860d0f' (length=39)
      'label' => string 'Select People Type' (length=18)
      'name' => string 'acf[field_5953fae740968][0][field_59e61ff30ff19_field_5c2d0d1860d0f]' (length=68)
      'prefix' => string 'acf[field_5953fae740968][0]' (length=27)
      'type' => string 'taxonomy' (length=8)
      'value' => int 123
      'menu_order' => int 5
      'instructions' => string '' (length=0)
      'required' => int 0
      'id' => string '' (length=0)
      'class' => string '' (length=0)
      'conditional_logic' => 
        array (size=1)
          0 => 
            array (size=1)
              0 => 
                array (size=3)
                  ...
      'parent' => string 'field_5953fae740968' (length=19)
      'wrapper' => 
        array (size=3)
          'width' => string '' (length=0)
          'class' => string '' (length=0)
          'id' => string '' (length=0)
      '_name' => string 'peopletype_taxonomy_list' (length=24)
      '_prepare' => int 1
      '_valid' => int 1
      'taxonomy' => string 'furman-person-type' (length=18)
      'field_type' => string 'select' (length=6)
      'allow_null' => int 0
      'add_term' => int 1
      'save_terms' => int 0
      'load_terms' => int 0
      'return_format' => string 'id' (length=2)
      'multiple' => int 0
      '_clone' => string 'field_59e61ff30ff19' (length=19)
      'parent_layout' => string '59e61fe0a4879' (length=13)
      '__key' => string 'field_5c2d0d1860d0f' (length=19)
      '__name' => string 'peopletype_taxonomy_list' (length=24)
      '__label' => string 'Select People Type' (length=18)
  • 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;
    
        });
    
    });
Viewing 10 posts - 1 through 10 (of 10 total)