/** * Indents any object as long as it has a unique id and that of its parent. * * @since 1.0 * * @param type $array * @param type $parentId * @param type $parentKey * @param type $selfKey * @param type $childrenKey * @return array Indented Array */ public static function array_nest($array, $parentId = 0, $parentKey = 'post_parent', $selfKey = 'ID', $childrenKey = 'children') { $indent = array(); // clean counter $i = 0; foreach ($array as $v) { if ($v->{$parentKey} == $parentId) { $indent[$i] = $v; $indent[$i]->{$childrenKey} = CACIE_Arrays::array_nest($array, $v->{$selfKey}, $parentKey, $selfKey, $childrenKey); $i++; } } return $indent; }
/** * Get post parent columns * * @since 1.0 * * @return array Parent post options ([post ID] => [post title]) */ public function get_post_parent_options() { $options = array(); $posts_query = new WP_Query(array('post_type' => $this->storage_model->key, 'posts_per_page' => -1)); if ($posts_query->have_posts()) { $nestedposts = CACIE_Arrays::array_nest($posts_query->posts, 0, 'post_parent', 'ID', 'cacie_children'); $indentedposts = CACIE_Arrays::convert_nesting_to_indentation($nestedposts, 'post_title', 'cacie_children'); foreach ($indentedposts as $post) { $options[$post->ID] = $post->post_title; } } return $options; }