Skip to content

Commit

Permalink
get all posts helper
Browse files Browse the repository at this point in the history
  • Loading branch information
andrefelipe committed Nov 18, 2020
1 parent 496d5f7 commit 44039dc
Showing 1 changed file with 30 additions and 28 deletions.
58 changes: 30 additions & 28 deletions src/PostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,32 +113,34 @@ function () {
}


// idea, but still would need to allow order options
// just think of the naming getAll or all
// also consider if the caching is disabled

// public static function getAll(array $params = [])
// {
// return Cache::php(
// static::$post_type . '/all' . (!empty($params) ? '-' . Str::slug($params) : ''),
// -1,
// function () use ($params) {
// $query_args = [
// 'post_type' => static::$post_type,
// 'posts_per_page' => -1,
// 'post_status' => 'publish',
// 'no_found_rows' => true,
// 'update_post_meta_cache' => false,
// 'update_post_term_cache' => false,
// 'orderby' => 'menu_order title',
// 'order' => 'ASC',
// // order as class vars? could help to set the archive columns too
// ];
// $query_args = array_merge($query_args, $params);
// $query = new \WP_Query($query_args);

// return Cast::posts($query->posts);
// }
// );
// }
public static function all(array $params = []): Posts
{
$fn = function () use ($params) {
$query_args = [
'post_type' => static::$post_type,
'posts_per_page' => -1,
'post_status' => 'publish',
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'orderby' => 'date',
'order' => 'DESC',
// order as class vars? could help to set the archive columns too
];
$query_args = array_merge($query_args, $params);
$query = new \WP_Query($query_args);

return Cast::posts($query->posts);
};

if (config('cache.enabled')) {
$cache_key = static::$post_type
. '/all'
. (!empty($params) ? '-' . Str::slug($params) : '');

return Cache::php($cache_key, -1, $fn);
}

return $fn();
}
}

0 comments on commit 44039dc

Please sign in to comment.