1

I am attempting to get the latest article in a category that has a number of subcategories through the Articles ContentModel.

If I use $model->setState('filter.category_id', $this->category->id);, it does not return anything due to the category in question not containing any articles; all of the articles are in subcategories.

How do I get all articles in the current category including children?

Code in question:

$model = JModelLegacy::getInstance('Articles', 'ContentModel', array('ignore_request' => true));
$model->getState();

// Set application parameters in model
$app = JFactory::getApplication();
$appParams = $app->getParams();
$model->setState('params', $appParams);

// Set the filters based on the module params
$model->setState('list.start', 0);
$model->setState('list.limit', 1);
$model->setState('filter.category_id', $this->category->id);
$model->setState('filter.published', 1);

// Permissions
$access = !JComponentHelper::getParams('com_content')->get('show_noauth');
$authorised = JAccess::getAuthorisedViewLevels(JFactory::getUser()->get('id'));
$model->setState('filter.access', $access);

// Featured Item

// Get article from featured set
// $model->setState('filter.featured', 'only');

// Ordering
$model->setState('list.ordering', 'a.publish_up');
$model->setState('list.direction', 'DESC');

$items = $model->getItems();
1

You need to enable subcategories with the subcategories.filter and also specify the levels of subcategories you want to go deep down.

$model->setState('filter.subcategories', true);
$model->setState('filter.max_category_levels', $subCategoryLevels);

Note that the subcategories check will occur, if the category id for the filter.category_id is numeric, otherwise if you pass an array of category ids (e.g. coming from a multiple choice select field), then the model will not look for subcategories, but it will load the articles for the given categories ids.

  • Perfect. I tried digging through com_content but nothing stood out to me. Thanks! – Mark May 19 '17 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.