3

I have created some custom fields in my joomla article from backend now I want to fetch them in another component. What would be the code to achieve it?

  • Do you know how it is possible to retrieve Articles by FieldValues? – LiliwoL Jan 24 '18 at 12:21
5

You can use the following code to fetch custom field values by article id

<?php       
        JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php'); //load fields helper
        $customFieldnames = FieldsHelper::getFields('com_content.article', $article_id, true); // get custom field names by article id
        $customFieldIds = array_map(create_function('$o', 'return $o->id;'), $customFieldnames); //get custom field Ids by custom field names
        $model = JModelLegacy::getInstance('Field', 'FieldsModel', array('ignore_request' => true)); //load fields model
        $customFieldValues = $model->getFieldValues($customFieldIds , $item->content_item_id); //Fetch values for custom field Ids
?>

Hope it will help you.

  • Do you know how it is possible to retrieve Articles by FieldValues? – LiliwoL Jan 24 '18 at 12:21
  • But you have not defined $article_id (and a spelling mistake). How do you find the article ID? – Eoin Jul 6 '18 at 23:07
  • Ok here is how you get the active article ID ` $app = JFactory::getApplication(); // Access the Application Object $menu = $app->getMenu(); // Load the JMenuSite Object $active = $menu->getActive(); // Load the Active Menu Item as an stdClass Object $article_id = $active->id;` – Eoin Jul 6 '18 at 23:25

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.