Get Article Field Values From Anywhere
The following code snippets use Joomla's Database Classes to retrieve information about any article in the __content table of your Joomla database.
Note that every function requires you to use Factory. So be sure to include the use statement at the top of the file you're using any of these functions in.
Remember, querying the database can be expensive if done too often. Personally, I use these functions in cached modules, so they really don't happen very often. These functions are good for querying info about individual articles based on the given article ID. If you want to query a lot of articles at once or in rapid succession there are better ways to do this. For most use cases, though, I imagine the functions below will be sufficient.
1use Joomla\CMS\Factory;
Article Intro Text
This function will retrieve the intro text of any article by the article's ID number. I use similar functions when creating modules to display featured articles and such.
1use Joomla\CMS\Factory;
2
3/**
4 *
5 * @param int $articleId The article id we want to get the intro text for
6 * @return string The intro text for the article
7 */
8function getArticleIntrotextById($articleId)
9{
10 // Get a database connection
11 $db = Factory::getContainer()->get('DatabaseDriver');
12 // Create a new query object
13 $query = $db->getQuery(true);
14 // Select fields
15 $query->select($db->quoteName('introtext'));
16
17 // From the content (articles) table
18 $query->from($db->quoteName('#__content'));
19
20 // Where the article id matches
21 $query->where($db->quoteName('id') . ' = ' . $db->quote($articleId));
22
23 // Reset the query using our newly populated query object
24 $db->setQuery($query);
25
26 // Get the result and return the intro text as a string
27 $result = $db->loadResult();
28 return $result;
29}
Get Fulltext By Id
The following function is nearly identical to the previous one which retrieves introtext, but this gets all the text after the "Read More" page break.
Note that if an article doesn't have a readmore page break, this won't return any text since it's all technically in the introtext field.
1/**
2 *
3 * @param int $articleId The article id we want to get the full text for
4 * @return string The full text for the article
5 */
6function getArticleFullText($articleId)
7{
8 // Get a database connection
9 $db = Factory::getContainer()->get('DatabaseDriver');
10 $query = $db->getQuery(true);
11
12 // Select the fulltext field
13 $query->select($db->quoteName('fulltext'));
14
15 // From the content (articles) table
16 $query->from($db->quoteName('#__content'));
17 $query->where($db->quoteName('id') . ' = ' . $db->quote($articleId));
18 $db->setQuery($query);
19
20 $result = $db->loadResult();
21
22 return $result;
23}
Get Entire Article
You could just use the two above functions to get the entire article, or you can retrieve the introtext and fulltext together and return the combined result.
1/**
2 * This function gets the introtext and fulltext for an article and returns it as a string
3 * @param int $articleId The article id we want to get the text for
4 *
5 */
6function getEntireArticle($articleId){
7
8 $db = Factory::getContainer()->get('DatabaseDriver');
9 $query = $db->getQuery(true);
10
11 // Select the introtext and fulltext fields
12 $query->select($db->quoteName(array('introtext', 'fulltext')));
13
14 // From the content (articles) table
15 $query->from($db->quoteName('#__content'));
16
17 // Where the article id matches
18 $query->where($db->quoteName('id') . ' = ' . $db->quote($articleId));
19
20 // Reset the query using our newly populated query object
21 $db->setQuery($query);
22
23 // Load introtext and fulltext, then return combined string
24 $result = $db->loadAssoc();
25 return $result['introtext'] . $result['fulltext'];
26
27}
You can use the above examples to get info for any field in the content table (catid, alias, title, etc.)
Article Images
Article images (not the ones in the article HTML itself, but the Intro Image and the Full Article Image) are stored together as JSON data in the images field of the content table. So while it's easy to get this information from the DB, formatting it properly for display requires taking another step.
1function getArticleIntroImgSrc($articleId){
2
3 //get the db
4 $db = JFactory::getContainer()->get('DatabaseDriver');
5
6 //get the images
7 $query = $db->getQuery(true);
8 $query->select($db->quoteName('images'));
9 $query->from($db->quoteName('#__content'));
10 $query->where($db->quoteName('id') . ' = '. $articleId);
11 $db->setQuery($query);
12 $result = $db->loadResult();
13
14 //decode the json
15 $images = json_decode($result);
16
17 //get the intro image
18 $introImg = $images->{'image_intro'};
19 return $introImg;
20
21}
In that form, you could display it like this:
1//display article intro image for id 3
2<img src="/<?php echo(getArticleIntroImgSrc(3));?>"/>
Every Attribute
A more generic method that retrieves any field from the images field.
1function getArticleImageProps($articleId){
2
3 //get the db
4 $db = JFactory::getContainer()->get('DatabaseDriver');
5
6 //get the images
7 $query = $db->getQuery(true);
8 $query->select($db->quoteName('images'));
9 $query->from($db->quoteName('#__content'));
10 $query->where($db->quoteName('id') . ' = '. $articleId);
11 $db->setQuery($query);
12 $result = $db->loadResult();
13
14 //decode the json
15 $images = json_decode($result);
16
17 //return all the properties
18 return $images;
19
20}
Here are all the fields or attributes you can access using the above method:
- image_intro
- float_intro
- image_intro_alt
- image_intro_caption
- image_fulltext
- image_fulltext_alt
- fulltext_caption
For example, if you wanted to get the intro img src and alt text, you could use this:
1<?php
2//getting img from article id 3
3$imageDetails = getArticleImageProps(3);
4$imgSrc = $imageDetails->image_intro;
5$imgAlt = $imageDetails->image_intro_alt;
6$imgTitle = $imageDetails->image_intro_caption;
7?>
8
9<img src="/<?php echo $imgSrc; ?>" alt="<?php echo $imgAlt; ?>" title="<?php echo $imgTitle; ?>" />
Category Name and Link
The content table has a catid field for category id, but it doesn't include the name of the category. So you need to either run two queries or join the tables on the category id.
1function getCategoryLink($articleId){
2
3 $db = JFactory::getContainer()->get('DatabaseDriver');
4
5 //get the category name and id
6 $query = $db->getQuery(true);
7 $query->select($db->quoteName('b.title'));
8 $query->select($db->quoteName('b.id'));
9
10 $query->from($db->quoteName('#__content', 'a'));
11 //join
12 $query->join('INNER', $db->quoteName('#__categories', 'b') . ' ON (' . $db->quoteName('a.catid') . ' = ' . $db->quoteName('b.id') . ')');
13 $query->where($db->quoteName('a.id') . ' = '. $articleId);
14 $db->setQuery($query);
15 $result = $db->loadObject();
16
17 //return the link
18 $link = JRoute::_(ContentHelperRoute::getCategoryRoute($result->id));
19 $catTitle = $result->title;
20
21 return '<a href="'.$link.'">'.$catTitle.'</a>';
22
23}
The above code will render a link to the category an article is in based on the article ID.
Spotted a mistake? Select the sentence in the guide and choose “Suggest a fix”.
Related guides
Adding Custom Options to your Joomla Template
In this guide, you will learn how to add custom fields/options to the Joomla template you designed. This guide assumes you already know the basics of Joomla…
Adding Custom Joomla Module Chrome (Styles)
Module Chromes are added by creating a new php file for each chrome/style under your template's directory at…
Creating Joomla Content Plugins (With Examples)
In this guide, you will learn the structure of a basic Joomla Content Plugin. We'll explore several examples, so you can create your own content plugins from…

Comments
Found an error? instead, it reaches me faster.No comments yet. Questions and corrections are welcome.