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.

use Joomla\CMS\Factory;Code language: PHP (php)

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.

use Joomla\CMS\Factory;

/**
 *
 * @param int $articleId The article id we want to get the intro text for
 * @return string The intro text for the article
 */
function getArticleIntrotextById($articleId)
{
    // Get a database connection
    $db = Factory::getContainer()->get('DatabaseDriver');
    // Create a new query object
    $query = $db->getQuery(true);
    // Select fields
    $query->select($db->quoteName('introtext'));

    // From the content (articles) table
    $query->from($db->quoteName('#__content'));

    // Where the article id matches
    $query->where($db->quoteName('id') . ' = ' . $db->quote($articleId));

    // Reset the query using our newly populated query object
    $db->setQuery($query);

    // Get the result and return the intro text as a string
    $result = $db->loadResult();
    return $result;
}Code language: PHP (php)

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.

/**
 *
 * @param int $articleId The article id we want to get the full text for
 * @return string The full text for the article
 */
function getArticleFullText($articleId)
{
    // Get a database connection
    $db = Factory::getContainer()->get('DatabaseDriver');
    $query = $db->getQuery(true);

    // Select the fulltext field
    $query->select($db->quoteName('fulltext'));

    // From the content (articles) table
    $query->from($db->quoteName('#__content'));
    $query->where($db->quoteName('id') . ' = ' . $db->quote($articleId));
    $db->setQuery($query);

    $result = $db->loadResult();

    return $result;
}Code language: PHP (php)

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.

/**
 * This function gets the introtext and fulltext for an article and returns it as a string
 * @param int $articleId The article id we want to get the text for
 *
 */
function getEntireArticle($articleId){

    $db = Factory::getContainer()->get('DatabaseDriver');
    $query = $db->getQuery(true);

    // Select the introtext and fulltext fields
    $query->select($db->quoteName(array('introtext', 'fulltext')));

    // From the content (articles) table
    $query->from($db->quoteName('#__content'));

    // Where the article id matches
    $query->where($db->quoteName('id') . ' = ' . $db->quote($articleId));

    // Reset the query using our newly populated query object
    $db->setQuery($query);

    // Load introtext and fulltext, then return combined string
    $result = $db->loadAssoc();
    return $result['introtext'] . $result['fulltext'];

}Code language: PHP (php)

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.

function getArticleIntroImgSrc($articleId){

    //get the db
    $db = JFactory::getContainer()->get('DatabaseDriver');

    //get the images
    $query = $db->getQuery(true);
    $query->select($db->quoteName('images'));
    $query->from($db->quoteName('#__content'));
    $query->where($db->quoteName('id') . ' = '. $articleId);
    $db->setQuery($query);
    $result = $db->loadResult();

    //decode the json
    $images = json_decode($result);

    //get the intro image
    $introImg = $images->{'image_intro'};
    return $introImg;

}Code language: PHP (php)

In that form, you could display it like this:

//display article intro image for id 3
<img src="/<?php echo(getArticleIntroImgSrc(3));?>"/>Code language: HTML, XML (xml)

Every Attribute

A more generic method that retrieves any field from the images field.

function getArticleImageProps($articleId){

    //get the db
    $db = JFactory::getContainer()->get('DatabaseDriver');

    //get the images
    $query = $db->getQuery(true);
    $query->select($db->quoteName('images'));
    $query->from($db->quoteName('#__content'));
    $query->where($db->quoteName('id') . ' = '. $articleId);
    $db->setQuery($query);
    $result = $db->loadResult();

    //decode the json
    $images = json_decode($result);

    //return all the properties
    return $images;

}Code language: PHP (php)

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:

<?php
//getting img from article id 3
$imageDetails = getArticleImageProps(3);
$imgSrc = $imageDetails->image_intro;
$imgAlt = $imageDetails->image_intro_alt;
$imgTitle = $imageDetails->image_intro_caption;
?>

<img src="/<?php echo $imgSrc; ?>" alt="<?php echo $imgAlt; ?>" title="<?php echo $imgTitle; ?>" />Code language: HTML, XML (xml)

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.

function getCategoryLink($articleId){

    $db = JFactory::getContainer()->get('DatabaseDriver');

    //get the category name and id
    $query = $db->getQuery(true);
    $query->select($db->quoteName('b.title'));
    $query->select($db->quoteName('b.id'));

    $query->from($db->quoteName('#__content', 'a'));
    //join
    $query->join('INNER', $db->quoteName('#__categories', 'b') . ' ON (' . $db->quoteName('a.catid') . ' = ' . $db->quoteName('b.id') . ')');
    $query->where($db->quoteName('a.id') . ' = '. $articleId);
    $db->setQuery($query);
    $result = $db->loadObject();

    //return the link
    $link = JRoute::_(ContentHelperRoute::getCategoryRoute($result->id));
    $catTitle = $result->title;

    return '<a href="'.$link.'">'.$catTitle.'</a>';

}Code language: PHP (php)

The above code will render a link to the category an article is in based on the article ID.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments