Home › Forums › Backend Issues (wp-admin) › Using publish_posttype to get acf fields
I am working on a feature, that when a post for my custom post type is first published it will retrieve both WP fields like post_title and my custom acf fields. I have tried acf/save_post but this is only getting the acf fields and I need wordpress fields too. I’ve tried using get_post_meta and currently working with getting the acf fields via $_POST. If basically kill my function and dump all the variables I can see all the data. But when capturing the $_POST in a variable it’s returning NULL. I should also mention that I am capturing the fields to pass to an xml file using simpleXML. The xml file is getting generated and I can see all the normal WP fields just can’t seem to capture any of the ACF stuff. Here is all of my logic that I am using:
public function generateDataXML($ID, $post)
{
$upload = wp_upload_dir();
$upload_dir = $upload['basedir'];
$upload_dir = $upload_dir . '/events';
if ( ! file_exists( $upload_dir ) ) {
wp_mkdir_p( $upload_dir );
}
$xml = new \SimpleXMLElement('<rss xmlns:g="http://base.google.com/ns/1.0" />', LIBXML_NOERROR, false, 'g', true);
$xml->addAttribute('version', '2.0');
$channel = $xml->addChild('channel');
$channel->addChild('title', get_bloginfo('name'));
$channel->addChild('link', get_bloginfo('url'));
$channel->addChild('description', get_bloginfo('description'));
$eventID = $post->ID;
$postName = $post->post_name;
$posttitle = $post->post_title;
$abspath = get_site_url();
if (get_post_status($eventID) == 'publish') {
//Web Data Tab
//If no Artist Name then will capture Event Name
$artistNameID = $_POST['acf']['field_5f223fa5381da']['field_5f223fd2381db'];
$artistNamePost = get_post($artistNameID);
$artistName = $artistNamePost->post_title;
$primaryGenre = get_post_meta($artistNameID, 'genres_primary_genre', true);
$secondaryGenre = get_post_meta($artistNameID, 'genres_secondary_genre', true);
$eventName = $posttitle;
$eventDate = $_POST['acf']['field_5f2241e2c0bf4']['field_5f224203c0bf5'];
$doorsOpen = $_POST['acf']['field_5f2241e2c0bf4']['field_5f2242b5c0bf7'];
$eventImageArray = intval($_POST['acf']['field_5f2241e2c0bf4']['field_5f245777ac9b5']);
$image = wp_get_attachment_image_src($eventImageArray, 'full');
//die(var_dump($_POST['acf']['field_5f2241e2c0bf4']));
//Campaing Data
//If Campaign Data has data fields not null it will use this instead of web data
$campaignArtistName = intval(get_post_meta($eventID, 'artist_info_ad_campaign_artist_name_ad_campaign'));
$campaignEventName = get_post_meta($eventID, 'show_info_ad_campaign_event_name_ad_campaign');
$campaignEventDate = get_post_meta($eventID, 'show_info_ad_campaign_date_of_event_ad_campaign', true);
$campaignDoorsOpen = get_post_meta($eventID, 'show_info_ad_campaign_doors_open_time_ad_campaign', true);
$campaignEventImageArray = get_post_meta($eventID, 'show_info_ad_campaign_show_event_image_ad_campaign', true);
$campaignImage = wp_get_attachment_image_src($campaignEventImageArray, 'full');
$eventRow = $channel->addChild('item');
$eventRow->addAttribute("id", $postName.'-'.$eventID);
$eventRow->addChild("g:g:title", htmlspecialchars($posttitle));
if ($artistNameID) {
$eventRow->addChild('g:g:artist', $artistName);
} else {
if (!empty($campaignEventName)) {
$eventRow->addChild('g:g:event', $campaignEventName);
} else {
$eventRow->addChild('g:g:artist', $eventName);
}
}
if ($primaryGenre) {
$eventRow->addChild('g:g:primarygenres', $primaryGenre);
}
if($secondaryGenre) {
$eventRow->addChild('g:g:secondarygenres', $secondaryGenre);
}
if (!empty($campaignEventDate)) {
$eventRow->addChild('g:g:event', date('F j, Y', strtotime($campaignEventDate)));
} else {
$eventRow->addChild('g:g:event', date('F j, Y', strtotime($eventDate)));
}
if (!empty($campaignDoorsOpen)) {
$eventRow->addChild('g:g:opens', date("g:iA", strtotime($campaignDoorsOpen)));
} else {
$eventRow->addChild('g:g:opens', date("g:iA", strtotime($doorsOpen)));
}
if (!empty($campaignEventImage)) {
$eventRow->addChild('g:g:image', '<img src="'.$campaignImage[0].'" />');
} else {
$eventRow->addChild('g:g:image', '<img src="'.$image[0].'" />');
}
}
$file = trailingslashit( $upload_dir ) . 'events-data.xml';
$open = fopen($file, 'w') or die ("File cannot be opened.");
$dom = new \DOMDocument("1.0");
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
fwrite($open, $dom->saveXML());
fclose($open);
}
The post fields that you’re talking about are not fields but data in the post table. When using acf/save_post
you need to $post = get_post($post_id)
and then access the post information from there like $title = $post->post_title
. Or you can skip getting the post and use something like $title = get_the_title($post_id)
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!
Accordions are a great way to group related information while allowing users to interactively show and hide content. In this video, Damon Cook goes in-depth on how to create an accessible accordion block using ACF PRO’s Repeater field.https://t.co/RXT0g25akN
— Advanced Custom Fields (@wp_acf) March 2, 2023
© 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.