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 Answer 1

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?

5
  • Hmmmm, that is very interesting
    – Kuubs
    Commented May 3, 2021 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
    – Kuubs
    Commented May 4, 2021 at 8:44
  • Have you assigned the 'MyTagName' to an article?
    – mfleeson
    Commented May 4, 2021 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.
    – Kuubs
    Commented May 5, 2021 at 9:15
  • 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]);
    – Kuubs
    Commented May 5, 2021 at 9:36

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.