@return array The most recently modified posts. */ public function get_recent_posts( string $post_type, int $limit, string $search_filter = '', bool $disable_excluding_old_posts = false ): array { $exclude_older_than_one_year = false; if ( $post_type === 'post' && ! $disable_excluding_old_posts ) { $exclude_older_than_one_year = true; } if ( $this->indexable_helper->should_index_indexables() ) { return $this->get_recently_modified_posts_indexables( $post_type, $limit, $exclude_older_than_one_year, $search_filter ); } return $this->get_recently_modified_posts_wp_query( $post_type, $limit, $exclude_older_than_one_year, $search_filter ); } /** * Returns most recently modified posts of a post type, using indexables. * * @param string $post_type The post type. * @param int $limit The maximum number of posts to return. * @param bool $exclude_older_than_one_year Whether to exclude posts older than one year. * @param string $search_filter Optional. The search filter to apply to the query. * * @return array The most recently modified posts. */ private function get_recently_modified_posts_indexables( string $post_type, int $limit, bool $exclude_older_than_one_year, string $search_filter = '' ): array { $posts = []; $recently_modified_indexables = $this->indexable_repository->get_recently_modified_posts( $post_type, $limit, $exclude_older_than_one_year, $search_filter ); foreach ( $recently_modified_indexables as $indexable ) { $indexable_meta = $this->meta->for_indexable( $indexable ); if ( $indexable_meta->post instanceof WP_Post ) { $posts[] = Content_Type_Entry::from_meta( $indexable_meta ); } } return $posts; } /** * Returns most recently modified posts of a post type, using WP_Query. * * @param string $post_type The post type. * @param int $limit The maximum number of posts to return. * @param bool $exclude_older_than_one_year Whether to exclude posts older than one year. * @param string $search_filter Optional. The search filter to apply to the query. * * @return array The most recently modified posts. */ private function get_recently_modified_posts_wp_query( string $post_type, int $limit, bool $exclude_older_than_one_year, string $search_filter = '' ): array { $args = [ 'post_type' => $post_type, 'posts_per_page' => $limit, 'post_status' => 'publish', 'orderby' => 'modified', 'order' => 'DESC', 'has_password' => false, ]; if ( $exclude_older_than_one_year === true ) { $args['date_query'] = [ [ 'after' => '12 months ago', ], ]; } if ( $search_filter !== '' ) { $args['s'] = $search_filter; } $posts = []; foreach ( \get_posts( $args ) as $post ) { $posts[] = Content_Type_Entry::from_post( $post, \get_permalink( $post->ID ) ); } return $posts; } }