/** * Get a collection of catalog items from the database. May * possibly return an empty array. * * @param array $categories The array of category slugs to filter with. Will be ignored if false. * @param string $operator Operator used to test the categories filter. Possible values are 'IN', 'NOT IN' or 'AND'. * @param string $sort The database field used to sort the collection. Possible values are 'title', 'order', 'date' or 'random'. * @param string $order The order the collection should be returned in. Possible values are 'asc' or 'desc'. * @param integer $offset The start ordinal of the collection, or number of catalog items to skip over. * @param integer $limit The maximum amount of catalog items allowed in the collection. * @param boolean $load_categories Wether to load the category ids for each item of the collection. Possible large database hit. * @return array An array of CataBlogItem objects */ public static function getItems($categories = false, $operator = 'IN', $sort = 'menu_order', $order = 'asc', $offset = 0, $limit = -1, $load_categories = true) { $items = array(); $cata = new CataBlogItem(); $params = array('post_type' => $cata->getCustomPostName(), 'orderby' => $sort, 'order' => $order, 'offset' => $offset, 'numberposts' => $limit); if (!empty($categories)) { $tax_query_array = array(array('taxonomy' => $cata->getCustomTaxName(), 'field' => 'id', 'terms' => $categories, 'operator' => $operator)); $params['tax_query'] = $tax_query_array; } // $query = new WP_Query(); // $query->query($params); // $posts = $query->posts; // echo $query->post_count; // echo $query->found_posts; $posts = get_posts($params); // return an array of CataBlogItems foreach ($posts as $post) { $item = new CataBlogItem(); $item->id = $post->ID; $item->title = $post->post_title; $item->description = $post->post_content; $item->date = $post->post_date; $item->categories = array(); $item->order = $post->menu_order; $item->_post_name = $post->post_name; $item_cats = array(); if ($load_categories) { $category_ids = array(); $terms = get_the_terms($post->ID, $item->_custom_tax_name); if (is_array($terms)) { foreach ($terms as $term) { $category_ids[$term->term_id] = $term->name; } } $item->categories = $category_ids; } $meta = get_post_meta($post->ID, $item->_post_meta_name, true); $item->processPostMeta($meta); $items[] = $item; } return $items; }