0

I am working with the publish_posttype action to get data of post as it's getting published. I pushing these values to an xml file using simpleXML. I can get things like post title easily since that is stored in the $post object, but I also need to get the values of my custom fields which are ACF fields. I've done some research and since publish action is fired before the meta is saved in the db. If I var_dump my values I can see them just fine, storing them in variables to pass into addChild nodes is coming up empty. When I also look at my error log it looks like php is seeing these values as Undefined index. See my logic below:

$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;
    //die(var_dump(get_post_meta($eventID)));
    $eventDate = get_field( "date_of_event", $eventID );
    $postName = $post->post_name;
    $posttitle = $post->post_title;
    $abspath = get_site_url();
    $showInfo = $_POST['acf']['field_5f2241e2c0bf4'];
    $campaignShowInfo = $_POST['acf']['field_5f2458fc9d313'];
    $eventDate = $showInfo['field_5f224203c0bf5'];
    $campaignEventDate = $campaignShowInfo['field_5f2458fc9d315'];
    $doorsOpen = $showInfo['field_5f2242b5c0bf7'];
    $campaignDoorsOpen = $campaignShowInfo['field_5f2458fc9d317'];
    $tourName = $showInfo['field_5f22400c381dc'];
    $compaignTourName =  $campaignShowInfo['field_5f2458f49d30f'];
    $eventImage = intval($showInfo['field_5f245777ac9b5']);
    $campaignEventImage = intval($campaignShowInfo['field_5f2458f49d30f'];
    $image = wp_get_attachment_image_src($eventImage, 'full');
    $campaignImage = wp_get_attachment_image_src($campaignEventImage, 'full');

    $eventRow = $channel->addChild('item');
    $eventRow->addAttribute("id", $postName.'-'.$eventID);
    $eventRow->addChild("g:g:title", $posttitle);

    if (!empty($compaignTourName)) {
        $eventRow->addChild('g:g:tour', $compaignTourName);
    } else {
        $eventRow->addChild('g:g:tour', $tourName);
    }

    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', $campaignDoorsOpen);
    } else {
        $eventRow->addChild('g:g:opens', $doorsOpen);
    }

    if ($campaignEventImage) {
        $eventRow->addChild('g:g:image', $campaignImage[0]);
    } else {
        $eventRow->addChild('g:g:image', $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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.