BlogHelper::getRelatedPosts PHP Method

getRelatedPosts() public method

同じタグの関連投稿を取得する
public getRelatedPosts ( array $post, array $options = [] ) : array
$post array ブログ記事
$options array オプション(初期値 : array()) - `recursive` : 関連データを取得する場合の階層(初期値 : -1) - `limit` : 件数(初期値 : 5) - `order` : 並び順指定(初期値 : BlogPost.posts_date DESC)
return array
    public function getRelatedPosts($post, $options = array())
    {
        if (empty($post['BlogTag'])) {
            return array();
        }
        $options = array_merge(array('recursive' => -1, 'limit' => 5, 'order' => 'BlogPost.posts_date DESC'), $options);
        $tagNames = array();
        foreach ($post['BlogTag'] as $tag) {
            $tagNames[] = urldecode($tag['name']);
        }
        $BlogTag = ClassRegistry::init('Blog.BlogTag');
        $tags = $BlogTag->find('all', array('conditions' => array('BlogTag.name' => $tagNames), 'recursive' => 1));
        if (!isset($tags[0]['BlogPost'][0]['id'])) {
            return array();
        }
        $ids = array_unique(Hash::extract($tags, '{n}.BlogPost.{n}.id'));
        $BlogPost = ClassRegistry::init('Blog.BlogPost');
        $conditions = array(array('BlogPost.id' => $ids), array('BlogPost.id <>' => $post['BlogPost']['id']), 'BlogPost.blog_content_id' => $post['BlogPost']['blog_content_id']);
        $conditions = am($conditions, $BlogPost->getConditionAllowPublish());
        // 毎秒抽出条件が違うのでキャッシュしない
        $relatedPosts = $BlogPost->find('all', array('conditions' => $conditions, 'recursive' => $options['recursive'], 'order' => $options['order'], 'limit' => $options['limit'], 'cache' => false));
        return $relatedPosts;
    }