2

In my news entries, I have a lightswitch field with the handle 'pinned'. I'm trying to query my news section for both pinned and unpinned news entries (true or false essentially), but I can't get consistent results with any of the following:

{% set pinnedNews = craft.entries().section('news').pinned('1').all() %}
{% set unpinnedNews = craft.entries().section('news').pinned('0').all() %}

{% set pinnedNews = craft.entries().section('news').pinned('true').all() %}
{% set unpinnedNews = craft.entries().section('news').pinned('false').all() %}

{% set pinnedNews = craft.entries().section('news').pinned('1').all() %}
{% set unpinnedNews = craft.entries().section('news').pinned('not 1').all() %}

{% set pinnedNews = craft.entries().section('news').search('pinned:1').all() %} THIS WORKS
{% set unpinnedNews = craft.entries().section('news').search('pinned:0').all() %} DOES NOT WORK
{% set unpinnedNews = craft.entries().section('news').search('pinned:not 1').all() %} DOES NOT WORK

Is there a proper to get the boolean values in Craft 3?

3

true/false are booleans, not strings, so you shouldn't surround them in quotes:

{% set pinnedNews = craft.entries().section('news').pinned(true).all() %}
{% set unpinnedNews = craft.entries().section('news').pinned(false).all() %}
  • 1
    very good yes programming 101! I promise you this...I set it to (false) then retried, cleared cache, retried, still missing one "false" entry. So I went to that entry, flipped the switch on and off, resaved, tested, bingo. It wasn't until i resaved that i got the correct result. Thank you! – Jim May 21 at 2:29
  • Huh very interesting. Glad it worked! 🙌 – Jalen Davenport May 21 at 2:47
  • I've read others have had trouble with switches "sticking". I'll try to find these. – Jim May 21 at 3:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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