Support

Account

Home Forums Backend Issues (wp-admin) Added Taxonomy does not get linked to Field

Solved

Added Taxonomy does not get linked to Field

  • Hi,

    i’m currently working on a Importer plugin which works correctly but struggles with the taxonomies. They get all saved correctly with wp_set_object_terms and in the backend you can see the ACF fields set correctly but when i make a var_dump on the frontend, the meta fields aren’t visible.

    Not until i hit the Update-Button on the newly imported post i’m able to see the set taxonomy.

    This is the whole function that gets executed when i make an import

    Any help would be awesome! Thanks

    public function trr_import_entries()
    	{
    		$this->check_nonce();
    		$data = json_decode(stripslashes($_POST['data']), true);
    		$postTypes = array(
    			'people' => array(
    				'post_type' => 'person',
    				'link' => 'people_id',
    				'title' => array('title_pre', 'first_name', 'last_name', 'title_post'),
    			),
    			'projects' => array(
    				'post_type' => 'project',
    				'link' => 'project_id',
    				'title' => 'title',
    			),
    			'publications' => array(
    				'post_type' => 'publication',
    				'link' => 'publication_id',
    				'title' => 'T1'
    			),
    			'events' => array(
    				'post_type' => 'tribe_events',
    				'link' => 'event_id',
    				'title' => 'title',
    			),
    		);
    
    		foreach ($data['selected_entries'] as $entryType => $entries) {
    			$entryLink = $postTypes[$entryType]['link'];
    			$entryPostType = $postTypes[$entryType]['post_type'];
    			$entryTitle = $postTypes[$entryType]['title'];
    
    			foreach ($entries as $entry) {
    				$id = $this->getPostByLink($entry[$entryLink], $entryPostType);
    
    				if ($id !== 0) {
    					if (!$this->isUpdateable($id, $entry['updated_at'])) {
    						continue;
    					}
    				}
    
    				if (is_array($entryTitle)) {
    					$title = '';
    					foreach ($entryTitle as $item) {
    						if (!empty($entry[$item])) {
    							$title .= $entry[$item] . ' ';
    						}
    					}
    				} else {
    					$title = $entry[$entryTitle];
    				}
    
    				$data = [
    					'ID' => $id,
    					'post_title' => $title,
    					'post_type' => $entryPostType,
    					'post_status' => 'publish'
    				];
    
    				$post = wp_insert_post($data);
    
    				// Update ACF fields
    				update_field('db_import_link', $entry[$entryLink], $post);
    				update_field('db_import_updated_at', $entry['updated_at'], $post);
    
    				// Fields configured in the Backend
    				$postFields = get_field('db_import_entries_' . $entryType, 'option');
    				if (!empty($postFields)) {
    					foreach ($postFields as $entries) {
    						foreach ($entries as $field) {
    						$fieldParent = $field['entry_parent'];
    							if (empty($fieldParent)) {
    								update_field($field['entry'][0]['entry_fieldname'], $entry[$field['entry'][0]['entry_name']], $post);
    							} else {
    								$fieldGroup = [];
    								foreach ($field['entry'] as $fieldEntry) {
    									$fieldGroup[$fieldEntry['entry_fieldname']] = $entry[$fieldEntry['entry_name']];
    								}
    								update_field($fieldParent, $fieldGroup, $post);
    							}
    						}
    					}
    				}
    
    				if ($entryType == 'publications') {
    					if (isset($entry['PY']) && !empty($entry['PY'])) {
    						wp_set_object_terms($post, $entry['PY'], 'pub-year');
    					}
    					if (isset($entry['T2']) && !empty($entry['T2'])) {
    						wp_set_object_terms($post, $entry['T2'], 'pub-journal');
    					}
    
    					if (isset($entry['TY']) && !empty($entry['TY'])) {
    						$pubType = $entry['TY'];
    						if ($pubType == 'JOUR') {
    							wp_set_object_terms($post, 'Journal publication', 'pub-type');
    						}
    						if ($pubType == 'CHAP') {
    							wp_set_object_terms($post, 'Chapter', 'pub-type');
    						}
    					}
    
    					if (isset($entry['AB']) && !empty($entry['AB'])) {
    						$postData = array(
    							'ID'			=> $post,
    							'post_content' 	=> $entry['AB'],
    
    						);
    						wp_update_post( $postData );
    					}
    
    					if ($peopleLinks = $this->trr_get_table('people_publications', array('key' => 'publication_id', 'val' => get_field('db_import_link', $post)))) {
    						foreach ($peopleLinks as $link) {
    							$personID = $this->getPostByLink($link->people_id, 'person');
    							if ($personID) {
    								update_field('authors', $personID, $post);
    							}
    						}
    					}
    
    					if ($projectLinks = $this->trr_get_table('projects_publications', array('key' => 'publication_id', 'val' => get_field('db_import_link', $post)))) {
    						foreach ($projectLinks as $link) {
    							$projectID = $this->getPostByLink($link->project_id, 'project');
    							if ($projectID) {
    								update_field('related_projects', $projectID, $post);
    							}
    						}
    					}
    				}
    
    			}
    		}
    
    		wp_die('Finished');
    	}
  • Okay i fixed it by actually updating the corresponding field. Makes sense.. Lol

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Added Taxonomy does not get linked to Field’ is closed to new replies.