I'm trying to exclude entries that are tagged "DO NOT PROMOTE" on any page where craft.entries is used minus their actual entry page.

What I've come up with so far is this:

public function init()

        Exclude DO NOT PROMOTE tagged entries
    // Get DO NOT PROMOTE tag id
    $tagCriteria = craft()->elements->getCriteria(ElementType::Tag);
    $tagCriteria->title = 'DO NOT PROMOTE';
    $tag = $tagCriteria->first();

    // Get Entries related to above tag
    $entryCriteria = craft()->elements->getCriteria(ElementType::Entry);
    $entryCriteria->limit = null;
    $entryCriteria->status = null;
    $entryCriteria->relatedTo = [
        'targetElement' => $tag,
        'field' => 'tags'
    $doNotPromoteEntries = $entryCriteria->ids();

    craft()->on('elements.beforeBuildElementsQuery', function(Event $event) use ($doNotPromoteEntries) {
        $criteria = $event->params['criteria'];

        $criteria->id = 'and, not ' . implode(', not ', $doNotPromoteEntries);

Now this kind of works. The issue I'm running into is the homepage is now showing an entry post... which is should not be. So, my modifying the criteria has caused this issue.

Is there a more eloquent or better way to accomplish this?

  • What is an "entry post" I don't really know what you mean by that? Oct 8 '18 at 20:59
  • @RobinSchambach The homepage is setup to just show all recent entries. But with the above code, it's showing a single entry on the homepage... the actual entry template is used too. Which I find odd.
    – Erik
    Oct 8 '18 at 21:02
  • Well if the recent entries are entries with that tag they won't be fetched Oct 8 '18 at 21:05
  • That's what I am trying to do... I do not want anything showing up with that tag. And maybe I'm going about this the wrong way.
    – Erik
    Oct 8 '18 at 21:08

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.