<p>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.</p>

<p>What I've come up with so far is this:</p>

<pre><code>public function init()
{
    parent::init();

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

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

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

        $criteria-&gt;id = 'and, not ' . implode(', not ', $doNotPromoteEntries);
    });
}
</code></pre>

<p>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.</p>

<p>Is there a more eloquent or better way to accomplish this?</p>

CC BY-SA 4.0
2018-10-08T20:59:53.483Z
What is an "entry post" I don't really know what you mean by that?
Robin Schambach
CC BY-SA 4.0
2018-10-08T21:02:54.977Z
@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
CC BY-SA 4.0
2018-10-08T21:05:31.990Z
Well if the recent entries are entries with that tag they won't be fetched
Robin Schambach
CC BY-SA 4.0
2018-10-08T21:08:18.930Z
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
<plugin-development><entries><craft.entries>
Excluding entries based on a tag across whole site
# Excluding entries based on a tag across whole site

(Asked by: username_0 on 2018-10-08)

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()
{
    parent::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?

**Tags:** plugin-development, entries, craft.entries

> **Comments:**
> **username_1**: What is an "entry post" I don't really know what you mean by that?  
> **username_0**: @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.  
> **username_1**: Well if the recent entries are entries with that tag they won't be fetched  
> **username_0**: 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.  

---

## 0 answers

