public function getRelated(Post $post, $limit = 5, $images = true)
    {
        key_exists_or_fail(['id', 'tags'], $post->toArray(), __d('me_cms', 'ID or tags of the post are missing'));

        $cache = sprintf('related_%s_posts_for_%s', $limit, $post->id);
        if ($images) {
            $cache .= '_with_images';
        }

        return Cache::remember($cache, function () use ($images, $limit, $post) {
            $related = [];

            if ($post->has('tags')) {
                //Sorts and takes tags by `post_count` field
                $tags = collection($post->tags)->sortBy('post_count')->take($limit)->toList();

                //This array will be contain the ID to be excluded
                $exclude[] = $post->id;

                //For each tag, gets a related post.
                //It reverses the tags order, because the tags less popular have
                //  less chance to find a related post
                foreach (array_reverse($tags) as $tag) {
                    $post = $this->queryForRelated($tag->id, $images)
                        ->where([sprintf('%s.id NOT IN', $this->getAlias()) => $exclude])
                        ->first();

                    //Adds the current post to the related posts and its ID to the
                    //  IDs to be excluded for the next query
                    if ($post) {
                        $related[] = $post;
                        $exclude[] = $post->id;
                    }
                }
            }

            return $related;
        }, $this->getCacheName());
    }
public function getRelated(Post $post, $limit = 5, $images = true)
    {
        key_exists_or_fail(['id', 'tags'], $post->toArray(), __d('me_cms', 'ID or tags of the post are missing'));

        $cache = sprintf('related_%s_posts_for_%s', $limit, $post->id);
        if ($images) {
            $cache .= '_with_images';
        }

        return Cache::remember($cache, function () use ($images, $limit, $post) {
            $related = [];

            if ($post->has('tags')) {
                //Sorts and takes tags by `post_count` field
                $tags = collection($post->tags)->sortBy('post_count')->take($limit)->toList();

                //This array will be contain the ID to be excluded
                $exclude[] = $post->id;

                //For each tag, gets a related post.
                //It reverses the tags order, because the tags less popular have
                //  less chance to find a related post
                foreach (array_reverse($tags) as $tag) {
                    $post = $this->queryForRelated($tag->id, $images)
                        ->where([sprintf('%s.id NOT IN', $this->getAlias()) => $exclude])
                        ->first();

                    //Adds the current post to the related posts and its ID to the
                    //  IDs to be excluded for the next query
                    if ($post) {
                        $related[] = $post;
                        $exclude[] = $post->id;
                    }
                }
            }

            return $related;
        }, $this->getCacheName());
    }
public function getRelated ( Post $ post , $ limit = 5 , $ images = true ) { key_exists_or_fail ( [ 'id' , 'tags' ] , $ post -> toArray ( ) , __d ( 'me_cms' , 'ID or tags of the post are missing' ) ) ; $ cache = sprintf ( 'related_%s_posts_for_%s' , $ limit , $ post -> id ) ; if ( $ images ) { $ cache .= '_with_images' ; } return Cache :: remember ( $ cache , function ( ) use ( $ images , $ limit , $ post ) { $ related = [ ] ; if ( $ post -> has ( 'tags' ) ) { //Sorts and takes tags by `post_count` field $ tags = collection ( $ post -> tags ) -> sortBy ( 'post_count' ) -> take ( $ limit ) -> toList ( ) ; //This array will be contain the ID to be excluded $ exclude [ ] = $ post -> id ; //For each tag, gets a related post. //It reverses the tags order, because the tags less popular have //  less chance to find a related post foreach ( array_reverse ( $ tags ) as $ tag ) { $ post = $ this -> queryForRelated ( $ tag -> id , $ images ) -> where ( [ sprintf ( '%s.id NOT IN' , $ this -> getAlias ( ) ) => $ exclude ] ) -> first ( ) ; //Adds the current post to the related posts and its ID to the //  IDs to be excluded for the next query if ( $ post ) { $ related [ ] = $ post ; $ exclude [ ] = $ post -> id ; } } } return $ related ; } , $ this -> getCacheName ( ) ) ; }
Gets the related posts for a post
@param \MeCms\Model\Entity\Post $post Post entity. It must contain `id` and `Tags`
@param int $limit Limit of related posts
@param bool $images If `true`, gets only posts with images
@return array Array of entities
@uses queryForRelated()
@uses $cache
Gets the related posts for a post