Support

Account

Home Forums Search Search Results for 'taxonomy'

Search Results for 'taxonomy'

reply

  • You need to specify the id value that ACF needs to get the field https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

    
    <?php 
      $categories= get_the_category();
      if (!empty($categories)) {
        $term_id = $categories[0]->term_id;
        $image = get_field('category-image-field', 'term_'.$term_id);
        // what you do from here depends on what the field is returning
      }
    ?>
    
  • This piece of code work for me, setting the location to “Taxonomy equals <<whatever attribute>>”. Note that, as variations are loaded dynamically, javascript events are not attached to fields. Thus, using the gallery or image does not work: click on buttons does nothing. Trying to find a fix…

    $GLOBALS['wc_loop_variation_id'] = null;
    
    function is_field_group_for_variation($field_group, $variation_data, $variation_post) {
    	return (preg_match( '/Variation/i', $field_group['title'] ) == true);
    }
    
    add_action( 'woocommerce_product_after_variable_attributes', function( $loop_index, $variation_data, $variation_post ) {
    	$GLOBALS['wc_loop_variation_id'] = $variation_post->ID;
    
    	foreach ( acf_get_field_groups() as $field_group ) {
    		if ( is_field_group_for_variation( $field_group, $variation_data, $variation_post ) ) {
    			acf_render_fields( $variation_post->ID, acf_get_fields( $field_group ) );
    		}
    	}
    
    	$GLOBALS['wc_loop_variation_id'] = null;
    }, 10, 3 );
    
    add_action( 'woocommerce_save_product_variation', function( $variation_id, $loop_index ) {
    	if ( !isset( $_POST['acf_variation'][$variation_id] ) ) {
    		return;
    	}
    
    	$_POST['acf'] = $_POST['acf_variation'][$variation_id];
    
    	acf()->input->save_post( $variation_id );
    }, 10, 2 );
    
    add_filter( 'acf/prepare_field', function ( $field ) {
    	if ( !$GLOBALS['wc_loop_variation_id'] ) {
    		return $field;
    	}
    
    	$field['name'] = preg_replace( '/^acf\[/', 'acf_variation[' . $GLOBALS['wc_loop_variation_id'] . '][', $field['name'] );
    
    	return $field;
    }, 10, 1);
  • Hi,

    This piece of code may help you. To use it, add a location to your field group where Taxonomy equals your attribute name. Please note, this only work with “basic” fields, ie. not relying on javascript (gallery or image buttons do not work).

    $GLOBALS['wc_loop_variation_id'] = null;
    
    function is_field_group_for_variation($field_group, $variation_data, $variation_post) {
    	return (preg_match( '/Variation/i', $field_group['title'] ) == true);
    }
    
    add_action( 'woocommerce_product_after_variable_attributes', function( $loop_index, $variation_data, $variation_post ) {
    	$GLOBALS['wc_loop_variation_id'] = $variation_post->ID;
    
    	foreach ( acf_get_field_groups() as $field_group ) {
    		if ( is_field_group_for_variation( $field_group, $variation_data, $variation_post ) ) {
    			acf_render_fields( $variation_post->ID, acf_get_fields( $field_group ) );
    		}
    	}
    
    	$GLOBALS['wc_loop_variation_id'] = null;
    }, 10, 3 );
    
    add_action( 'woocommerce_save_product_variation', function( $variation_id, $loop_index ) {
    	if ( !isset( $_POST['acf_variation'][$variation_id] ) ) {
    		return;
    	}
    
    	$_POST['acf'] = $_POST['acf_variation'][$variation_id];
    
    	acf()->input->save_post( $variation_id );
    }, 10, 2 );
    
    add_filter( 'acf/prepare_field', function ( $field ) {
    	if ( !$GLOBALS['wc_loop_variation_id'] ) {
    		return $field;
    	}
    
    	$field['name'] = preg_replace( '/^acf\[/', 'acf_variation[' . $GLOBALS['wc_loop_variation_id'] . '][', $field['name'] );
    
    	return $field;
    }, 10, 1);
  • I have looked at this and I’m seeing the same thing. I cannot figure out any way to work around this issue. Please submit a bug report on this https://www.advancedcustomfields.com/contact/

    Actually, I do have a solution, however it may not be for everyone. My solution is that I remove the field groups from this page and I never make anything required and substitute some default for every field. This requires adding a custom value acf/location/rule_values/taxonomy and match acf/location/rule_match/taxonomy rules for taxonomy, unfortunately, I cannot find the relevant code on any of my sites right now. https://www.advancedcustomfields.com/resources/custom-location-rules/

  • i just revert to ACF PRO 5.7.3 and the same issue is there. Whenever i touch any taxonomy field, my custom field group is disapearing.

  • Update
    I was able to get the categories to appear by using this code, but I would still like to find a way to group these categories together based on what ACF field (from the Select Field option) was chosen. With this code, it is appearing as:

    ACF Field A
    Woocommerce category name A

    ACF Field B
    Woocommerce cateogry name B

    ACF Field A
    Woocommerce category name C

    Where I would prefer it look like this:

    ACF Field A
    Woocommerce category name A
    Woocommerce category name C

    ACF Field B
    Woocommerce category name B

    
    <?php 
    
      $args = array(
        'taxonomy' => 'product_cat',
        'hide_empty' => 0
      );
      $c = get_categories($args);
      $c_keep = array();
      foreach($c as  $cat){
        if (get_field('associated_collection', 'category_'.$cat->term_id)) {
          $c_keep[] = $cat; // forgot [] here
        }
      }
    
      foreach($c_keep as $cat){
        echo '<h1>'.get_field('associated_collection', 'category_'.$cat->term_id).'</h1>';
        echo '<p>'.$cat->name.'</p>';
      }
    
    ?>
    

    Any ideas on what I’m missing?

  • Strange I have the same problem with 5.7.10… Cannot click on a drop down list in the admin, got the “Cannot read property ‘id’ of undefined”
    Maybe a conflict with another plugin or with new jquery… I’m on WordPress 5.0.3

    Just to be more precise. It’s a Taxonomy subfield in a repeater. Empty is allowed. I return the term ID (not the object). Was working perfect before WordPress/Acf update…

  • we are having the same issue with taxonomy terms checkboxes, sounds like the same situation , but we are on wp 4.9.9 as we were avoiding moving over to gutenberg. I’ve seen in the release notes for acf 5.7.10 an entry that says –

    ‘* Fix – Fixed JS error when selecting taxonomy terms within Gutenberg.’

    Am wondering if the fix might actually be causing issues in our case?

  • We have found the issue. In our site, there is a custom hierarchical taxonomy and in which, we have more than 10000 terms ( parent-child) in it. While adding ‘relationship’ field under a field group, ACF is trying to query all those terms for its filter options. Due to more no of terms, it took long time to load that filter options. That query become very expensive due to that parent child hierarchical terms.

    We hope it might help others who has similar issue with ACF relationship field and more no of parent-child terms.

  • Cleaned up basic customization below. The $_REQUEST['post_id'] is the same as the $post_id provided in the arguments.

    function custom_relationship_query( $args, $field, $post_id ) {
    	$args['tax_query'] = [
    		[
    			'taxonomy' => 'custom_taxonomy_name',
    			'field'    => 'term_id',
    			'terms'    => str_replace( 'term_', '', $post_id ),
    		],
    	];
    	return $args;
    }
    add_filter('acf/fields/relationship/query', 'custom_relationship_query', 10, 3);
  • This may be complicated to explain but i’ll give it my best shot.

    The background…

    I’m making a site whereby teachers from different schools can create posts about different students from different schools. Importantly, teachers may only be able to view/choose/check/tick students from the same school as themselves.

    I have created new user roles (and capabilities) for teachers according to the name of their school e.g.
    XYZ primary school teachers are assigned the user role: xyz and capability: xyz
    ABC highschool teachers are assigned the user role: abc and capability: abc

    I have ACF Pro and CPTUI plugin to create new custom taxonomies.

    I have created a taxonomy student that contains the names of all the students.

    I have created a taxonomy school that contains the names of the different schools.

    WIth ACF I have created a field group that assigns the school taxonomy to the student taxonomy. This means when I create or edit an individual student term the student has a school taxonomy radio button for me to choose which school the student is assigned/belongs to.

    I have also done the same to the users (teachers). I have created a field group that assigns the school taxonomy to the users. This means when I create or edit a user (teacher) the user has a school taxonomy radio button for me to choose which user the user/teacher is assigned/belongs to.

    As you can see the above field groups that assign the school taxonomy to the users and student taxonomy gives both the users/teachers and students a common link/taxonomy (school).

    I have created another field group assigned to a post with many other fields.

    This is where my question really starts….

    When a user/teacher creates a post one of the fields the user must tick is the student. However, all the student names are listed. I only want the students(terms) assigned with the same school taxonomy as the user creating the post to be visible in the list of students. In other words only populate the student taxonomy with those students(terms) that have the same school(term) as the user(teacher) creating the post.

    When creating or editing a post…

    IF current_user (has_term(XYZ) in_tax(school))
    THEN populate taxonomy(student) with (has_term(XYZ) in_tax(school))
    Another way may be to have a condition whereby…

    IF current_user (user_capability(xyz)
    THEN populate taxonomy(student) with (has_term(XYZ) in_tax(school))
    Either way is fine by me the important thing is to only display the student names that belong to the same school as the teachers creating the post.

  • Some other (new?) differences that I find helpful are:

    • the Relationship field has the option to show a Featured Image preview
    • the Relationship field has the option to add additional filters like Post Type and Taxonomy to the actual field (instead of just pre-filtering in the field settings)
  • more context:

    With the relationship field, I can only choose the categories one by one, rather than just choose “categories,” as you can with a Taxonomy custom field. So, as categories change, I’d have to adjust each relationship field, and there are MANY.

    Taxonomy custom field = filter by taxonomy TYPE

    Relationship custom field = filter by taxonomy TERM

    Need the relationship custom field to filter by ALL categories (and ONLY categories, not tags) without selecting each one manually.

  • Thanks, John. I realize now my question may be too convoluted to answer without more details. However, I was able to solve some issues by using a couple plugins to convert my old post types and taxonomies.

    The final issues remaining are:

    How can I update my old Posts to use their existing Featured Images as the value for a new ACF File field I created for all Posts. I have thousands of Posts that already have a Featured Image, now I just need to update the database so those same image attachments are applied to my new ACF File field.

    Additionally, the old database had a column for player names (it was a WordPress custom field). I need all those values to now be migrated to my new Player Taxonomy. This is less of an ACF question and more related to updating values in the database.

  • 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.

  • As far as select vs taxonomy field, sometimes my clients don’t get everything they’re wishing for. Functioning is a higher priority than making it work and while I do my best to make the admin experience as nice as I can for them, making it work overrides making it pretty, and my clients don’t get much say in the design of the admin.

  • 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 thought you were using a taxonomy field
    try using

    add_action( 'acf/render_field/key=field_5c2d0d1860d0f' ......

  • 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 );
    
  • Knowing what I know now, I would probably build something like this a different way, and I don’t know if you can make this change or not, but I thought I would suggest it. Dealing with using data from different sites in a multisite setup is always difficult.

    Anyway, this is what I would do.

    1) get rid of the taxonomy field
    2) Use a basic select field (or checkbox, radio field)
    3) Populate the choices of this field dynamically (term->term_id => term->term_name) https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/

    This solution lets you avoid the problems you’re facing with the taxonomy field.

  • Yes, prepare_field is probably the closes hook before it is rendered.

    I just had a look at the render_field function for the taxonomy field and found another place where there is likely an issue with the value not appearing.

    The render field function for the taxonomy field also cause ACF to check for valid terms. There are no hooks in the render field method of the field type that will let you filter this and show terms that do not exist on the current site.

    There are 2 options that I know of.

    The first would be to create your own field type based on the ACF taxonomy field and remove the code that tests the terms. https://www.advancedcustomfields.com/resources/creating-a-new-field-type/

    The second would be a bit of a hack and I don’t know if it will work.

    Use the acf/render_field hook hhttps://www.advancedcustomfields.com/resources/acf-render_field/. While it is undocumented on this page, this hook also includes the field name and field key variations that all of the other field hooks include.

    Use this hook to create 2 actions, one that is fired before ACF (priority < 10) and one that is fired after ACF (priority > 10). In the first hook, switch to the main site and in the second hook switch back to the current site.

  • 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.

  • hello,

    I have a form1 on page1 and a form2 on page2, i need to get the value submit by the forms, no matter for me if it is after on before the post is created in database.
    For now this is what i do but
    1. Don’t know if it’s the good hook
    2. Like in docs, i have hardcoded (field_5c33260621785) the fields to retrieve (is it no possible dynamically ?)
    3. In this function, how can i know if $_POST[‘acf’] comes from FORM1 or FORM2 ?
    4. THnaksss a lot !!!

    function my_acf_save_post( $post_id ) {
     	$fields = false;
      	if( isset($_POST['acf']) )
    	{
    // get taxonomy sent my form on PAGE A	
    $zones = $_POST['acf']['field_5c33260621785'];
             foreach ($zones as $z) {
             $term = get_term( $z, 'zones_de_marche' );
             $name .= $term->name ."\r\n"; 
             }
    
    	}
     
    $args = array( 
        'number_to' => '+33111111',
        'message' => $name
    ); 
    twl_send_sms( $args );
     
                     }
    
    add_action('acf/save_post', 'my_acf_save_post', 20);
Viewing 25 results - 1,401 through 1,425 (of 3,194 total)