4

Is it possible to search only in entries that have the own URLs option checked.

Now i've this code:

{% set query = craft.request.getParam('q') %}
{% set queryWords = query|split(' ') %}

{% paginate craft.entries({
    search: queryWords|join(' OR '),
    subLeft: true,
    subRight: true,
    order: 'score'
}).id('not ' ~ entry.id).limit(10) as pageInfo, items %}
Remi
  • 41
  • 2

4 Answers4

4

Got this solution from Andrew Welch/nystudio107 over on Discord:

Query for ':notempty:' in the URI, for example:

craft.entries().uri(':notempty:').all()

Docs: ':empty:' and ':notempty:'

missmatsuko
  • 106
  • 3
0

At the risk of oversimplifying your problem... just hard-code the sections with the "own URL" option checked.

{% paginate craft.entries({
    section: ['sectionA', 'sectionB'],
    ...

The "own URL" checkbox happens at the section level, and section configurations rarely change (once the site is fully developed).

Entries in this section have their own URLs

In theory, you already know which sections allow URLs, and can simply hard-code the section handles into your code.

Accepted values include a section handle, an array of section handles, or a SectionModel object.

Lindsey D
  • 23,974
  • 5
  • 53
  • 110
0

If at all possible, I'd probably try and use Lindsey's answer in his follow-up comment.

The alternative is, you'd have to create a plugin that grabbed every section and looped through them, checks their settings and see if they have the "Entries in the section have their own URLs" set. If so, collect an array of them and pass them back to the template via a Template Variable.

You don't want to do that on every request, though, since it's overhead and you'll probably end up caching the data, which at that point, you might as well have went with Lindsey's solution.

Brad Bell
  • 67,440
  • 6
  • 73
  • 143
0

I've found a solution.

{% paginate craft.entries({
    search: queryWords|join(' OR '),
    subLeft: true,
    subRight: true,
    order: 'score'
}).section('and, not sectionName').id('not ' ~ entry.id).limit(10) as pageInfo, items %}

When i add this .section('and, not sectionName') a section without urls is not included in the search.

Remi
  • 41
  • 2