Support » Plugin: Advanced Custom Fields: Extended » Taxonomy terms

  • Resolved ds7ilg7

    (@ds7ilg7)


    Hi Konrad,

    I am using multiple taxonomy terms fields within a form. I use ACFE’s post action to create a parent post, and then I create child posts in functions.php.

    Your taxonomy terms field is very helpful to isolate term selections. Within this form, I use them as part of the child post name, and also to set object terms to the post and to the children of the post.

    When I set the return value to ‘Term name’, it’s perfect and works as expected. However, I receive this error: PHP Notice: Undefined property: WP_Error::$taxonomy in /XXX/public_html/wp-content/plugins/acf-extended/includes/fields/field-taxonomy-terms.php on line 1128.

    When I set the return value to ‘Term ID’, it is also perfect and works as expected – the difference is that there are no errors.

    I’m certain the error occurs in the children posts. Can you please advise on what I should do to avoid this error?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    Can you please share screenshots and code you’re using to set the terms with that “Term ID” & “Term Name” return value? It looks like the system is trying to retrieve a term that doesn’t exists during the assignation.

    This notice can be fixed by adding a check in the ACFE Taxonomy Terms code, but I’m not sure why changing the return value would trigger it, since the field should use the actual Term ID (and not the name) in all the cases.

    Regards.

    Thread Starter ds7ilg7

    (@ds7ilg7)

    You are impressively quick with support!

    I’m sure you’re right about ‘the system is trying to retrieve a term that doesn’t exists during the assignation’. I believe your assumption is correct because the fields are in a repeater. The loop happens after the post is created. I perform 2 things in the child posts:

    1. Create a new post from each row of the repeater, then,
    2. Take the fields out of the repeater and into regular fields in the back end.

    I do this because the site relies extensively on the field values. Querying repeaters can be quite taxing.

    The code:

    // Create Multiple Truck Order posts from repeater, then move fields out of the repeater for easier queries
    // The first loop creates the post. Then each post is updated with it's OWN values - meaning there is no repeater in the new post id.
    function post_truck_details($post_id ){
    
    	// get the repeater
    	$values = get_field('truck_order_info', $post_id);
    	// loop through the repeater
    	foreach( $values as $value ) { 
    		
    	// set args
    	$new_post = array(
    		'post_title' =>  $value['truck_quantity'].' '.$value['eq_truck_type'].' '.$value['trailer_type'].' @ '.$value['truck_start_time'],	
    		'post_type' => 'truck_order',
    		'post_parent' => $post_id,		
    		'post_status'	=> 'draft',
       );      
    
    	// Post Truck Order
    		$new_post_id = wp_insert_post($new_post);		
    			wp_set_object_terms($new_post_id, get_term(482)->slug, 'truck-order-status', true);
    			wp_set_object_terms( $new_post_id, $eqtrucktype = $value ['eq_truck_type'], 'eq_cat', true );
    			wp_set_object_terms( $new_post_id, $trailertype = $value['trailer_type'], 'eq_cat', true );
    			wp_set_object_terms( $new_post_id, $hazmatend = $value['requires_hazmat_drivers'], 'cdl_endorsement', true );	
    
    	//Get and update fields outside of repeater 
    		$projname = get_field('project_name', $post_id);
    		$projcon = get_field ('project_contractor', $post_id );
    		$projpm = get_field('project_pm', $post_id);
    		$workdate = get_field('work_date', $post_id);
    
    			update_field( 'project_name', $projname, $new_post_id);
    			update_field( 'project_contractor', $projcon, $new_post_id );
    			update_field( 'project_pm', $projpm, $new_post_id );
    			update_field( 'work_date', $workdate, $new_post_id );
    		
    	// Move repeater field values to each post's regular field
    	if (have_rows('truck_order_info', $post_id)) {
    	while(have_rows('truck_order_info', $post_id)) {
    	the_row();
    		$eqtrucktypes = array();
    			foreach( $eqtrucktypes as $eqtrucktype );
    				$eqtrucktype = $value['eq_truck_type'];
    		$tractorselects = array();
    			foreach( $tractorselects as $tractorselect );
    				$tractorselect = $value['tractor_selection'];	
    		$trailertypes = array();
    			foreach( $trailertypes as $trailertype );
    				$trailertype = $value['trailer_type'];
    		$truckstarts = array();
    			foreach( $truckstarts as $truckstart );		
    				$truckstart = $value['truck_start_time'];
    		$quans = array();
    			foreach( $quans as $quan );
    				$quan = $value['truck_quantity'];
    		$loadloctypes = array();
    			foreach( $loadloctypes as $loadloctype );		
    				$loadloctype = $value['load_location_type'];
    		$loadlocsups = array();
    			foreach( $loadlocsups as $loadlocsup );		
    				$loadlocsup = $value['load_location_supplier'];	
    		$loadlocsrecyclers = array();
    			foreach( $loadlocsrecyclers as $loadlocsrecycler );		
    				$loadlocsrecycler = $value['load_location_recycler'];
    		$loadlocotherprojs = array();
    			foreach( $loadlocotherprojs as $loadlocotherproj );		
    				$loadlocotherproj = $value['load_location_other_project'];	
    		$loadlocsrecyclers = array();
    			foreach( $loadlocsrecyclers as $loadlocsrecycler );		
    				$loadlocsrecycler = $value['load_location_recycler'];
    		$loadlocothersups = array();
    			foreach( $loadlocothersups as $loadlocothersup );		
    				$loadlocothersup = $value['load_location_other_supplier'];
    		$altloadlocs = array();
    			foreach( $altloadlocs as $altloadloc );		
    				$altloadloc = $value['alternate_load_location'];
    		$descriptions = array();
    			foreach( $descriptions as $description );
    				$description = $value['load_description'];
    		$weights = array();
    			foreach( $weights as $weight );
    				$weight = $value['load_weight'];
    		$lengths = array();
    			foreach( $lengths as $length );
    				$length = $value['load_length'];			
    		$loadagtypes = array();
    			foreach( $loadagtypes as $loadagtype );		
    				$loadagtype = $value['load_side_aggregate_type'];	
    		$loadrecycleagtypes = array();
    			foreach( $loadrecycleagtypes as $loadrecycleagtype );		
    				$loadrecycleagtype = $value['load_side_rec_aggregate_type'];			
    		$mattypes = array();
    			foreach( $mattypes as $mattype );		
    				$mattype = $value['material_type'];	
    		$workonsites = array();
    			foreach( $workonsites as $workonsite );		
    				$workonsite = $value['work_onsite'];
    		$droploctypes = array();
    			foreach( $droploctypes as $droploctype );		
    				$droploctype = $value['drop_location_type'];
    		$droplocsups = array();
    			foreach( $droplocsups as $droplocsup );		
    				$droplocsup = $value['drop_location_supplier'];	
    		$droplocrecycles = array();
    			foreach( $droplocrecycles as $droplocrecycle );		
    				$droplocrecycle = $value['drop_location_recycler'];	
    		$droploclandfills = array();
    			foreach( $droploclandfills as $droploclandfill );		
    				$droploclandfill = $value['drop_location_landfill'];	
    		$droplocotherprojs = array();
    			foreach( $droplocotherprojs as $droplocotherproj );		
    				$droplocotherproj = $value['drop_location_other_project'];	
    		$droplocs = array();
    			foreach( $droplocs as $droploc );		
    				$droploc = $value['drop_location'];
    		$droplocothersups = array();
    			foreach( $droplocothersups as $droplocothersup );		
    				$droplocothersup = $value['drop_location_other_supplier'];
    		$confirmagpups = array();
    			foreach( $confirmagpups as $confirmagpup );		
    				$confirmagpup = $value['confirm_agg_pickup'];
    		$dropsideagtypes = array();
    			foreach( $dropsideagtypes as $dropsideagtype );		
    				$dropsideagtype = $value['drop_side_aggregate_type'];
    		$confirmrecycleagpups = array();
    			foreach( $confirmrecycleagpups as $confirmrecycleagpup );		
    				$confirmrecycleagpup = $value['confirm_rec_agg_pickup'];
    		$dropsiderecycleagtypes = array();
    			foreach( $dropsiderecycleagtypes as $dropsiderecycleagtype );		
    				$dropsiderecycleagtype = $value['drop_side_rec_aggregate_type'];
    		$contaminateds = array();
    			foreach( $contaminateds as $contaminated );		
    				$contaminated = $value['is_material_contaminated'];	
    		$hazmatends = array();
    			foreach( $hazmatends as $hazmatend );		
    				$hazmatend = $value['requires_hazmat_drivers'];			
    		$backhauls = array();
    			foreach( $backhauls as $backhaul );		
    				$backhaul = $value['back_haul_other'];
    		$bhothmats = array();
    			foreach( $bhothmats as $bhothmat );		
    				$bhothmat = $value['back_haul_other_material'];
    		$bhdescriptions = array();
    			foreach( $bhdescriptions as $bhdescription );		
    				$bhdescription = $value['backhaul_description'];	
    		$bhweights = array();
    			foreach( $bhweights as $bhweight );		
    				$bhweight = $value['backhaul_weight'];	
    		$bhlengths = array();
    			foreach( $bhlengths as $bhlength );		
    				$bhlength = $value['backhaul_length'];				
    		$addinsts = array();
    			foreach( $addinsts as $addinst );		
    				$addinst = $value['additional_instructions'];	
    		
    update_field( 'eq_truck_type', $eqtrucktype, $new_post_id );
    update_field( 'tractor_selection', $tractorselect, $new_post_id );		
    update_field( 'trailer_type', $trailertype,  $new_post_id );
    update_field( 'truck_start_time', $truckstart,  $new_post_id );	
    update_field( 'truck_quantity', $quan,  $new_post_id );		
    update_field( 'load_location_type', $loadloctype,  $new_post_id );
    update_field( 'load_location_supplier', $loadlocsup,  $new_post_id );
    update_field( 'load_location_recycler', $loadlocsrecycler,  $new_post_id );
    update_field( 'load_location_other_project', $loadlocotherproj,  $new_post_id );	
    update_field( 'load_location_other_supplier', $loadlocothersup,  $new_post_id );
    update_field( 'alternate_load_location', $altloadloc,  $new_post_id );
    update_field( 'load_description', $description,  $new_post_id );
    update_field( 'load_weight', $weight,  $new_post_id );
    update_field( 'load_length', $length,  $new_post_id );	
    update_field( 'load_side_aggregate_type', $loadagtype,  $new_post_id );		
    update_field( 'load_side_rec_aggregate_type', $loadrecycleagtype,  $new_post_id );
    update_field( 'material_type', $mattype,  $new_post_id );
    update_field( 'work_onsite', $workonsite,  $new_post_id );	
    update_field( 'drop_location_type', $droploctype,  $new_post_id );
    update_field( 'drop_location_supplier', $droplocsup,  $new_post_id );
    update_field( 'drop_location_recycler', $droplocrecycle,  $new_post_id );
    update_field( 'drop_location_landfill', $droploclandfill,  $new_post_id );
    update_field( 'drop_location_other_project', $droplocotherproj,  $new_post_id );
    update_field( 'drop_location', $droploc,  $new_post_id );
    update_field( 'drop_location_other_supplier', $droplocothersup,  $new_post_id );
    update_field( 'confirm_agg_pickup', $confirmagpup,  $new_post_id );
    update_field( 'drop_side_aggregate_type', $dropsideagtype,  $new_post_id );
    update_field( 'confirm_rec_agg_pickup', $confirmrecycleagpup,  $new_post_id );
    update_field( 'drop_side_rec_aggregate_type', $dropsiderecycleagtype,  $new_post_id );
    update_field( 'is_material_contaminated', $contaminated,  $new_post_id );
    update_field( 'requires_hazmat_drivers', $hazmatend,  $new_post_id );
    update_field( 'back_haul_other', $backhaul,  $new_post_id );
    update_field( 'back_haul_other_material', $bhothmat,  $new_post_id );
    update_field( 'backhaul_description', $bhdescription,  $new_post_id );
    update_field( 'backhaul_weight', $bhweight,  $new_post_id );
    update_field( 'backhaul_length', $bhlength,  $new_post_id );	
    update_field( 'additional_instructions', $addinst,  $new_post_id );	
    update_field( 'order_to_pm', $post_id,  $new_post_id );		
    			}			
    		}
    	}
    }
    add_filter('acfe/form/submit/post/form=new-truck-order', 'post_truck_details', 10, 5);
    
    Thread Starter ds7ilg7

    (@ds7ilg7)

    I apologize in advance – I thought I had the code in a module.

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Okay thanks! It’s hard to tell, because I don’t really know what’s behind the field names/variables, but you most likely make a get_field() somewhere and then update_field() on a Taxonomy Terms.

    The problem is that when you do get_field('my_terms', 126) it will format the value, and thus return the Term Names (if you configured the return value to Term Name). Then you take those names and update the other Taxonomy Terms field with update_field('my_other_terms', $term_names, 226).

    The thing is that the Taxonomy Terms field need Terms ID, and not names, to be correctly updated. This is why there is a PHP notice when you change the return value in the field configuration.

    You should retrieve the terms unformatted, using the following code: get_field('my_terms', 126, false). This way, you make sure that the return value format is not used, and you always get the DB value (Terms IDs).

    See get_field() documentation.

    I’ll add a check in the field source code in the next patch, to avoid this notice in case someone has the same use case as yours.

    Note: Format Value is an important concept in ACF logic, you should check your other fields in the code, maybe you make the same mistake somewhere else.

    Hope it helps!

    Have a nice day!

    Regards.

    Thread Starter ds7ilg7

    (@ds7ilg7)

    Yes, this does help – thank you. I’ll be able to take a look at it tomorrow and I’ll report back then.

    I appreciate your work and for the support you extend.

    Have a nice evening.

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Sure thing!

    Thanks for the support 🙂

    Have a nice evening.

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