/** * This returns all models sorted by Make Title, then Model Title * * @return \SquirrelsInventory\Model[] */ public static function getAllModels() { $models = array(); $makes = Make::getAllMakes(); $parents = array(0 => array()); foreach ($makes as $make_id => $make) { $parents[$make_id] = array(); } $query = new \WP_Query(array('post_type' => self::CUSTOM_POST_TYPE, 'post_status' => 'publish', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC')); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); $custom = get_post_custom(get_the_ID()); $model = new Model(get_the_ID(), get_the_title()); if (array_key_exists('make_id', $custom)) { $make_id = $custom['make_id'][0]; $model->setMakeId($make_id); if (array_key_exists($model->getMakeId(), $makes)) { $model->setMake($makes[$model->getMakeId()]); } } else { $make_id = 0; } $parents[$make_id][] = $model; } } foreach ($parents as $parent_id => $_models) { /** @var Model[] $_models */ foreach ($_models as $model) { $models[$model->getId()] = $model; } } return $models; }
/** * Make::getAllMakes updates the global $post variable, * which is why I'm assigning it to a temp variable below * * @param $column */ public function customModelColumns($column) { $post = $GLOBALS['post']; $makes = Make::getAllMakes(); $GLOBALS['post'] = $post; if ($column == 'make_id') { $make_id = get_post_meta($post->ID, 'make_id', TRUE); if (array_key_exists($make_id, $makes)) { echo $makes[$make_id]->getTitle(); } } }