Set custom attribute value

I have a Text type custom attribute named listing_primary_number.
I’m trying to populate the value for this attribute from the vendor’s primary_number attribute when submiting a new listing
I can successfully fetch the primary number but cannot set the custom attribute value whatever I try, it’s always empty.

This is my code:

add_filter(
	'hivepress/v1/forms/listing_update',
	function( $form ) {			
		if ( current_user_can('edit_posts') ) {							
			$vendor = HivePress\Models\Vendor::query()->filter(['user' => get_current_user_id(),])->get_first();
			if ( $vendor ) {
				$primary_number = $vendor->get_primary_number();
				if (isset($form['fields']['listing_primary_number'])) {
					$form['fields']['listing_primary_number']['default'] = $primary_number;
				} else {
					error_log('The field does not exist');
				}			
			}			
		}
		return $form;
	},
	1000
);

If i set the description to $primary_number, I see it in the description so I know it’s working.
But it doesn’t populate the text input

Thank you for waiting. Please try this PHP code snippet instead.

add_action(
	'hivepress/v1/models/listing/create', 
	function($listing_id) {
		$vendor = HivePress\Models\Vendor::query()->filter(['user' => get_current_user_id()])->get_first();
		
		if(!$vendor || !$vendor->get_primary_number()){
			return;
		}
		
		update_post_meta($listing_id, 'hp_listing_primary_number', $vendor->get_primary_number());
	}
);
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.