Find all the post records associated with a category

ENABLED, PUBLISH_ON <= TODAY, DESC

@param   $numberOfPostsToTake    The number of post records to select from the db
@return  collection
343405
public function getSomePublishablePosts($numberOfPostsToTake)
    {
        $collection = $this->getAll();

        $filtered = $collection->filter(function ($item) {
            if ($item->enabled == 1) return true;
        });

        $filtered = $filtered->filter(function ($item) {
            $todaysDate = DatesHelper::todaysDateSetToLocalTime();
            if ($item->publish_on <= $todaysDate) return true;
        });

        return $filtered
            ->sortByDesc('updated_at')
            ->take($numberOfPostsToTake)
        ;
    }
346635
final public function page($pageNumber, $recordsPerPage = null)
    {
        $recordsPerPage = $recordsPerPage ?? $this->recordsPerPage;

        $this->getQuery()->limit(((int)$pageNumber) * $recordsPerPage, $recordsPerPage);
        $this->pagedQuery = true;

        return $this;
    }
630008
public function count()
    {
        $this->preCount();
        $result = $this->dataStore->count();

        return $this->postCount($result);
    }
326491
protected function getCountCommon($data = array())
    {
        parent::getCountCommon($data);

        // --------------------------------------------------------------------------

        $oDb = Factory::service('Database');
        $oDb->select($this->tableAlias . '.*');

        if (!empty($data['include_count'])) {

            $subQuery = '
                SELECT
                    COUNT(DISTINCT post_id)
                FROM ' . NAILS_DB_PREFIX . 'blog_post_category
                WHERE
                category_id = ' . $this->tableAlias . '.id';

            $oDb->select('(' . $subQuery . ') post_count');
        }

        //  Default sort
        if (empty($data['sort'])) {

            $oDb->order_by($this->tableAlias . '.label');
        }
    }
1081357
private function appendListRowsFromEvents($paginationResult, string $postType)
    {
        $customListRows = Event::fire('post:table_list_rows', [$paginationResult, $postType]);

        foreach($customListRows as $customList){
            if(is_array($customList)) {
                foreach ($customList as $rowID => $rows) {
                    if (isset($paginationResult->items()[$rowID])) {
                        foreach ($rows as $key => $value) {
                            $paginationResult->items()[$rowID]->$key = $value;
                        }
                    }
                }
            }
        }

        return $paginationResult;
    }
635706
public function list_data( $is_valid = true, $per_page = null, $page = 1, $where = [], $orderby = null ) {
		/** @var \wpdb $wpdb */
		global $wpdb;
		$table      = $this->get_related_table_name();
		$limit      = $per_page;
		$page       = max( 1, $page );
		$table      = [
			[ $table, 't' ],
			[
				[ $wpdb->posts, 'p' ],
				'INNER JOIN',
				[
					't.post_id',
					'=',
					'p.ID',
				],
			],
		];
		$total      = $this->app->db->select_count( $table, null, $where );
		$total_page = isset( $per_page ) ? ceil( $total / $per_page ) : 1;
		$page       = min( $total_page, $page );
		$offset     = isset( $per_page ) && isset( $page ) ? $per_page * ( $page - 1 ) : null;
		if ( $is_valid ) {
			$where['p.post_status'] = 'publish';
		}

		$list = $this->app->db->select( $table, $where, null, $limit, $offset, $orderby );
		if ( empty( $list ) ) {
			return [
				'total'      => 0,
				'total_page' => 0,
				'page'       => $page,
				'data'       => [],
			];
		}

		$post_ids = $this->app->utility->array_pluck( $list, 'post_id' );
		$posts    = get_posts( [
			'include'   => $post_ids,
			'post_type' => $this->get_post_type(),
		] );
		$posts    = $this->app->utility->array_combine( $posts, 'ID' );

		return [
			'total'      => $total,
			'total_page' => $total_page,
			'page'       => $page,
			'data'       => array_map( function ( $d ) use ( $posts ) {
				return $this->filter_item( $this->set_post_data( $d, $posts[ $d['post_id'] ] ) );
			}, $list ),
		];
	}
1106826
public function getPosts($limit = false, $offset = false)
    {
        if (!$limit) {
            $limit = Option::get('posts_per_page');
        }
        return Post::getByAuthor($this->ID, $limit, $offset);
    }
62278
protected function getTotalOfRecordsString(int $totalOfRecords, array $data)
    {
        $show = $data['show'];
        $page = $data['page'];

        $startRecord = 0;
        if (intval($totalOfRecords) > 0) {
            $startRecord = ($show * ($page - 1)) + 1;
        }
        $endRecord = $show * $page;

        if ($endRecord > $totalOfRecords) {
            $endRecord = $totalOfRecords;
        }
        return $this->trans('totalOfRecordsString', [
            '%$startRecord%' => $startRecord,
            '%$endRecord%' => $endRecord,
            '%$totalOfRecords%' => $totalOfRecords,
        ], 'messages');
    }
880175
public function first() : ?Post
    {
        $params = array_merge($this->getParameters(), [
            'limit' => 1,
        ]);

        $posts = Timber::get_posts($params, $this->postClass);

        if (!is_array($posts)) {
            return null;
        }

        return collect($posts)->first();
    }
1000775
public function collectionAction($collection, Request $request = null)
    {
        $action = $this->container->get(CollectionPostArchiveAction::class);

        return $action($this->resolveRequest($request), $collection);
    }
1368661
public function getPostCountAttribute()
    {
    	if ( ! array_key_exists('postCount', $this->relations)) $this->load('postCount');
    
    	$related = $this->getRelation('postCount')->first();
    
    	return ($related) ? $related->aggregate : 0;
    }
793918
public function searchPosts()
    {
        $query = Vocabulary::find()->select('post_id, thread_id')->joinWith(['posts.author', 'posts.thread'])->andWhere(['is not', 'post_id', null]);
        if (Podium::getInstance()->user->isGuest) {
            $query->joinWith(['posts.forum' => function ($q) {
                $q->andWhere([Forum::tableName() . '.visible' => 1])->joinWith(['category' => function ($q) {
                    $q->andWhere([Category::tableName() . '.visible' => 1]);
                }]);
            }]);
        }
        if (!empty($this->query)) {
            $words = explode(' ', preg_replace('/\s+/', ' ', $this->query));
            $countWords = 0;
            foreach ($words as $word) {
                $query->orWhere(['like', 'word', $word]);
                $countWords++;
            }
            $query->groupBy('post_id');
            if ($this->match == 'all' && $countWords > 1) {
                $query->select(['post_id', 'thread_id', 'COUNT(post_id) AS c'])->having(['>', 'c', $countWords - 1]);
            }
        }
        $this->prepareQuery($query);
        $sort = [
            'defaultOrder' => ['post_id' => SORT_DESC],
            'attributes' => [
                'post_id' => [
                    'asc' => ['post_id' => SORT_ASC],
                    'desc' => ['post_id' => SORT_DESC],
                    'default' => SORT_DESC,
                ],
            ]
        ];
        return new ActiveDataProvider([
            'query' => $query,
            'sort' => $sort,
        ]);
    }
578367
public static function getNumberOfPostChildren($postOrPostId = null)
    {
        $postId = self::extractPostId($postOrPostId);
        if ($postId < 1) {
            return false;
        }

        global $wpdb;
        return (int) $wpdb->get_var(
            $wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_parent = %d", $postId)
        );
    }
1166077
public function findPages($unpublished = false)
    {
        $this->makeSelect();
        $this->sql .= " and collection_id is null";

        if (!$unpublished) {
            $this->sql .= " and published_date <= '{$this->today()}'";
        }

        return $this->find();
    }
281413
public function get_post_read_receipt_collection_vault() : post_read_receipt_collection_vault {
        return new post_read_receipt_collection_vault(
            $this->db,
            $this->entityfactory,
            $this->legacymapper->get_legacy_data_mapper_for_vault('post')
        );
    }
94097
public function getNextPost($categoryId, $publish_on)
    {
        return DB::table('posts')
            ->join('post_category', 'posts.id', '=', 'post_category.post_id')
            ->select('posts.slug', 'posts.title')
            ->where('post_category.category_id', '=', $categoryId)
            ->where('posts.enabled', '=', 1)
            ->where('posts.publish_on', '>', $publish_on)
            ->orderBy('publish_on', 'asc')
            ->first()
        ;
    }
850525
public function findEnabledAllPostsThatHaveCategoryId($catId)
    {
        $collection = $this->model->find($catId)->post->sortByDesc('updated_at');

        $filtered = $collection->filter(function ($item) {
            if ($item->enabled == 1) return true;
        });

        $filtered = $filtered->filter(function ($item) {
            $todaysDate = DatesHelper::todaysDateSetToLocalTime();
            if ($item->publish_on <= $todaysDate) return true;
        });


        // TODO: HAVE TO MANUALLY PAGINATE THE FILTERED COLLECTION :-(
        // https://laracasts.com/discuss/channels/laravel/laravel-pagination-not-working-with-array-instead-of-collection?page=1#reply-63860
        // http://www.reddit.com/r/laravel/comments/32kxn8/creating_a_paginator_manually/
        return $filtered->all();
    }