I am trying Joomla 4.
My biggest problem is to migrate the get article id functionality.

In Joomla 3.x, I used:

defined('_JEXEC') or die;    
$id= JRequest::getInt('id');

but this is not longer supported. So I now use:

use Joomla\CMS\Factory;
$app = Factory::getApplication();
$id   = $app->input->getCmd('id', '');

Is this really the best way to get the article id inside a PHP script?

  • 3
    Personally I'd use getInt() as opposed to getCmd() for stricter filtering. I don't know if there is a better way to get the article ID though
    – Lodder
    Jan 12, 2021 at 17:35
  • I face the same issue. $id = $app->input->getCmd('id', ''); gives indeed the article id... but only if you are on an Article View. If you are on a Blog View, it gives the ID of the category. In fact, I am creating an Alternate Layout of /components/com_fields/layouts/field/render.php to change the layout of some given Custom Fields. And there I need the ID of the article in order to give a unique id to the DIV. How to get that Article ID in a Blog view then? Sep 23, 2021 at 11:22
  • Marc while I am excited to have you as a contributor, I must maintain order and ensure that Stack Exchange's Q&A page design is upheld. You must not make "me too posts" as answers. You are welcome to post a new question with your_specific details. While JSE is in its growth phase (beta status), it is healthy and helpful to have _some overlap/redundancy in the questions pool. Eventually, the best-formed questions (not merely the oldest ones) with the best collection of answers will be treated as the "canonical" and all other duplicates will point to the canonical. Sep 23, 2021 at 22:57

1 Answer 1


Just to clarify, inside the article view, you can get the ID of the article:

$app = Factory::getApplication();
if (
    $app->input->get('option') === 'com_content'
    && $app->input->get('view') === 'article'
) {
    // Get the article ID
    $article_id = $app->input->getInt('id');

If you use the previous code inside a blog view, what you will get is the category ID.

Inside the blog view, you cant get the ID of a specific article just like before. In the blog view, you will need get the articles that category have, and then loop over them to do any task you want to accomplish. For this you will need build a query, something like:

$app = Factory::getApplication();
$factory = $app->bootComponent('com_content')->getMVCFactory();

// Get an instance of the generic articles model
$articles = $factory->createModel('Articles', 'Site', ['ignore_request' => true]);

// Get all the articles of a specific category, where $catid hold the ID of the category from where you want the articles
$articles->setState('filter.category_id', $catid);

You can apply others filters to that query as you need. When you get the articles from that query, then is when you can loop on each article and accomplish any task you want to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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