I wanted to get the number of articles inside category id=13.
How can I achieve this with PHP?
I've been searching and nothing.
Joomla Stack Exchange is a question and answer site for Joomla! administrators, users, developers and designers. It only takes a minute to sign up.
Sign up to join this communityI wanted to get the number of articles inside category id=13.
How can I achieve this with PHP?
I've been searching and nothing.
You can ask via SQL. It should be something like
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('COUNT(*)');
$query->from($db->quoteName('#__content'));
$query->where($db->quoteName('catid')." = 13";
// Reset the query using our newly populated query object.
$db->setQuery($query);
$count = $db->loadResult();
(This code is modified and from this source)
You probably also want to check whether the articles are published and/or visible by the user.
Try the following code:
$model = JModelLegacy::getInstance('Articles', 'ContentModel');
$model->setState('filter.category_id', 13); // Set category ID here
$articles = $model->getItems();
$num_articles = count($articles); // Returns the number of articles in category
$num_articles
will contain the number of articles in the given category (not including subcategories).