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 %}
2
  • What do you mean with "own URLs"? Commented Feb 24, 2017 at 11:25
  • Own urls is a option when you create a section
    – Remi
    Commented Feb 27, 2017 at 9:02

4 Answers 4

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:'

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.

2
  • We have 26 sections. The code would become very long when we do it this way.
    – Remi
    Commented Feb 27, 2017 at 7:49
  • 1
    In that case, I'd recommend creating a Twig global variable, to represent an array of all 26 sections. Then you could easily feed it into your queries as section: sectionsArray.
    – Lindsey D
    Commented Feb 27, 2017 at 8:03
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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.