1

I have the following challenge:

  • we have a news channel for multiple company locations.
  • Some news should be visible globally (for all locations), but some news are only visible for location X.

But when a visitor visits location-x.company.com/news/, both news entries should be visible:

  • news that are visible globally
  • OR
  • news that aren't visible globally, but have specific location X in the entries field

I have two fields in a channel:

  • Checkbox "Visible everywhere"? - yes / no

checkbox

  • If "Visible everyhwere" == false, an Entries Field is shown "Visible in locations: []"

checkbox false, another field shown - entries field

How can I generate a query in PHP for that?

I tried this, but it does not work - orWhere is ignored and it just ends up as "AND" in the SQL query.

$query->relatedTo(['targetElement' => $locationId, 'field' => 'visibleInLocations']);

$query->orWhere(['isVisibleInAllLocations' => true]);

The following does not work either ("Column not found: 1054 Unknown column 'relatedTo' in 'where clause'")

$query->andWhere([
    'or',
    ['relatedTo' => ['targetElement' => $locationId, 'field' => 'visibleInLocations']],
    ['isVisibleInAllLocations' => true],
]);

I made complex queries before, but not with relatedTo:

 $query->andWhere([
              'or',
              [
                  // for normal entries
                  'and',
                  ['=', $isProjectColumnName, 0],
                  ['>=', $minPriceColumn, $minPrice],
                  ['<=', $maxPriceColumn, $maxPrice],
              ],
              [
                  // for special project entries
                  'and',
                  ['=', $isProjectColumnName, 1],
                  // other fields for comparision
                  ['>=', $minPriceColumnForProjects, $minPrice],
                  ['<=', $minPriceColumnForProjects, $maxPrice],
              ]
          ]);

Has anybody solved this yet?

Thanks very much in advance for any hints! 🙏🙏🙏

(I searched almost everywhere, only found issues about chaining relatedTo or this quick hint https://craftcms.stackexchange.com/a/30348 - but don't know if this would be the easiest approach for my use case)

1 Answer 1

1

I did something similar to this in Twig this week. My query ended up looking like this based on the craft cms docs.

{% set query = craft.entries()
   .section('news')
   .search('isVisibleInAllLocations::true OR visibleInLocations:locationSlug)
   .status('live')
   .orderBy('postDate DESC')
   .all() 
%}

Please note I am using the search method for this, allowing me to pass parameters that either contain or match exactly. The :: means the field contains the exact value, and : means it contains part of that value. You can also use score as an orderBy in this case due to the usage of the search parameter, but news feeds tend to be most recent first.

I believe you would transfer the above Twig into the following PHP, please note that I haven't transfer Twig into PHP in a while

$query = Entry::find()
    ->section('news')
    ->search('isVisibleInAllLocations::true OR visibleInLocations:locationSlug')
    ->status('live')
    ->orderBy('postDate DESC')
    ->all();

I hope you find this helpful! Interested to know if this works for you or if you have an alternate solution.

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.