nystudio107 / craft-seomatic

SEOmatic facilitates modern SEO best practices & implementation for Craft CMS 3. It is a turnkey SEO system that is comprehensive, powerful, and flexible.
https://nystudio107.com/plugins/seomatic
Other
162 stars 68 forks source link

Exclude specific entry page from sitemap based on entry.customField? #1418

Closed tibbis closed 4 months ago

tibbis commented 4 months ago

Question

If I set a specific channel entry to noindex,nofollow in the twig code manually depending on a custom field (entry.enableIndexing) I don't want the entry page to be shown in the sitemap. The entry is part of a section channel that has sitemap enabled by default in SEOmate plugin.

How can I do this based on a field entry.enableIndexing?

I can't set robots to none in the plugin setting for that section as it will override all entries to none.

For now I only see a solution to have a separate section type for these pages instead and set robots to none in the plugin settings of that section type, but that is a bit messy.

khalwat commented 4 months ago

You can do this via PHP:

IncludeSitemapEntryEvent

The event that is triggered when an entry is about to be included in a sitemap.

use nystudio107\seomatic\events\IncludeSitemapEntryEvent;
use nystudio107\seomatic\helpers\Sitemap;
use yii\base\Event;

Event::on(
    Sitemap::class,
    Sitemap::EVENT_INCLUDE_SITEMAP_ENTRY,
    function(IncludeSitemapEntryEvent $event) {
        $event->include = false;
    }
);
khalwat commented 4 months ago

An easier way would also to include an SEO Settings field in the given Section, and instead of using a custom field to determine whether it is indexed or not, allow the content author to override the Robots field via that SEO Settings field.

That will eliminate the need for an extra custom field, automatically set the entry to not be indexed by Google et al via the <meta name="robots"> tag, and also exclude it from the sitemap.

tibbis commented 4 months ago

oh I didn't see that it added a new field type! thanks! One feature idea would then be to have the robots field in the section type to "Select from field" 😊