1

I'm trying to get all the articles from a specific category with a specific value in one of the custom fields (com_fields) I have created.

I have this code:

$model = JModelLegacy::getInstance('Articles', 'ContentModel', array('ignore_request' => true));
$model->setState('params', JFactory::getApplication()->getParams());
$model->setState('filter.category_id', 22);

$articles = $model->getItems();

This brings up 2 different problems.

  1. It gets all the articles from a specific category. But without the com_fields values
  2. This gets all the articles, and in adding the com_fields values I can filter these articles but the problem is that it put unnecessary load on the webserver. Is there some sort of better solution to only get the articles with the custom field value?
1

Looking at the source code for com_content - ArticlesModel does not have any filtering capabilities for custom fields. What it does have is tag filtering, so would it be possible to modify your code so that instead of writing a value to a custom field you instead assign a custom tag to the article which you can then use filter.tag when getting your ContentModel?

New contributor
mfleeson is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
5
  • Hmmmm, that is very interesting – Huub S May 3 at 14:58
  • How do I do this tho? I have a tag I have added and now I tried this: $model->setState('filter.tag', "MyTagName"); But that doesnt work – Huub S May 4 at 8:44
  • Have you assigned the 'MyTagName' to an article? – mfleeson May 4 at 11:15
  • I have found the problem. The filter tags works on ID of the tag, not the name. So I have to figure out how to get the id of the tag by name. – Huub S 2 days ago
  • I have found the solution: $filters = array('like' => "Tagname"); $results = TagsHelper::searchTags($filters); // note that this is a static function // Filer by tag $model->setState('filter.tag', [$results[0]->value]); – Huub S 2 days ago

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.