Home › Forums › Backend Issues (wp-admin) › 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
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
ποΈ Just one more day until the next session of ACF Chat Fridays! Make sure to register for the latest updates on whatβs coming in ACF 6.1!
— Advanced Custom Fields (@wp_acf) March 30, 2023
π Friday 31st March 3pm UTC
π Register here - https://t.co/3UtvQbDwNm pic.twitter.com/7xtEJakeQN
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.