Skip to content

Instantly share code, notes, and snippets.

@DrewDahlman
Created January 14, 2018 20:15
Show Gist options
  • Save DrewDahlman/b3964b554017889efdb76c1fe731f7a9 to your computer and use it in GitHub Desktop.
Save DrewDahlman/b3964b554017889efdb76c1fe731f7a9 to your computer and use it in GitHub Desktop.
<?php
/*
------------------------------------------
| parsePost:array (-)
|
| Parses a post object recursively.
|
| @post:obj - A wordpress post object
| @content:array - Array for content
| @nested:bool - Flag for 1 level deep nesting
------------------------------------------ */
function parsePost( $post, $content, $nested = false ){
// Setup the post content holder
$content = array();
// Essentials and base wordpress details
foreach( $post as $post_field_key => $post_field_value ){
$content[$post_field_key] = $post_field_value;
}
// Loop custom fields
if( get_fields( $post->ID ) ){
foreach( get_fields($post->ID) as $post_custom_field_key => $post_custom_field_value ){
// Check for a relationshop or a post object and process
if( get_field_object($post_custom_field_key, $post->ID)['type'] == "relationship" ){
if( gettype($post_custom_field_value) == "array" ){
foreach($post_custom_field_value as $post_related_key => $post_related_value ){
if( gettype($post_related_value) == "object" ){
$content[$post_custom_field_key][$post_related_key] = $this->parsePost($post_related_value, array(), true);
} else {
$content[$post_custom_field_key][$post_related_key] = $post_related_value;
}
}
}
} else if( get_field_object($post_custom_field_key, $post->ID)['type'] == "post_object" ){
if( gettype($post_custom_field_value) == "object" ){
$content[$post_custom_field_key] = $this->parsePost($post_custom_field_value, array(), true);
} else {
$content[$post_custom_field_key] = $post_custom_field_value;
}
} else {
$content[$post_custom_field_key] = $post_custom_field_value;
}
}
}
return $content;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment