1

I have an endpoint that I want to retrieve entries by a category, but then exclude entry ids if they have a certain tag. It seems to me that the relatedTo param overrides the id param that says not in include a set of ids. In general when using the ElementCriteriaModel is there a way to accomplish this?

'api/custom-endpoint.json' => function() {

    $tagSlugs = array('tag-one', 'tag-two');
    $tagIds = Tag::find()->group('tagGroup')->slug($tagSlugs)->ids();
    $categoryId = Category::find()->group('categoryGroup')->slug('category-slug')->ids();
    $excludeIds = Entry::find()->relatedTo($tagIds)->ids();

    return [
        'serializer' => 'jsonFeed',
        'elementType' => craft\elements\Entry::class,
        'criteria' => [
            'section' => 'blog',
            'id' => 'not ' . implode($excludeIds),
            'relatedTo' => [
                'targetElement' => $categoryId,
                'field' => 'categories'
            ],
        ],
        'transformer' => function(craft\elements\Entry $entry) {
            return [
                'title' => $entry->title,
                'url' => $entry->url,
            ];
        },
        'pretty' => true,
    ];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.