3

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.

  • Dear azhpo, part of your contribution in SE sites is to vote and accept those answers that help or completely answer you questions. This way you will help others that may have similar questions with you that a specific answer did solve the problem and at the same time it's a way to say thank you to those that took the time to help you. So please when an answer is the answer for you, remember to accept it. – FFrewin Feb 24 '15 at 14:50
2

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.

1

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).

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.