I need to filter posts so that I get posts only related to category. My url will be ../posts/index/4
Models:
My code for index is as follows:
public function index($cat_id = null)
{
$this->paginate = [
'contain' => ['Categories'],
'limit' => 2,
'conditions' =>['Posts.status' => 'publish'],
//'conditions' =>['PostsCategories.category_id' => '4'],
'order' => ['Posts.createdAt' => 'desc'],
'fields' => [
'Posts.id',
'Posts.title',
'Posts.excerpt',
'Posts.author',
'Posts.location',
'Posts.date',
'Posts.main_pic',
'Posts.hits'
],
];
$posts = $this->paginate($this->Posts);
$nextPage = $this->request->params['paging']['Posts']['nextPage'];
$this->set(compact('posts'));
$this->set(compact('nextPage'));
$this->set('_serialize', ['posts', 'nextPage']);
}