Hot answers tagged

5

{% set featuredProducts = craft.entries .section('sectionName') .featured(true) .all() %} Where sectionName is the name of your section; featured is your featured field handle; true is the value you want to query the lightswitch fields for. Each field type in the Craft docs has an element query example, e.g. lightswitch fields


5

There are a few different solutions for this. Personally my favourite is to switch on static caching in nginx, as it's a relatively quick and easy solution. Andrew has you covered on how to do that here: https://nystudio107.com/blog/static-caching-with-craft-cms If you're not using nginx, then a popular choice is the Blitz plugin: https://plugins.craftcms....


4

When looping over the letters in the range, you should be able to use the keys filter to check if the current letter is in the entriesByGroup array. If it is, you can link it or add a class, etc. {% set letterArray = 'A'..'Z' %} {% set entries = craft.entries.section('myEntries').orderBy('title asc').all() %} {% set entriesByGroup = entries|group('title[:1]')...


3

"Measure twice, cut once" goes the saying... (Kind of the opposite here, but the same principle!) - Fetch your data once, then cut it up how you like using Twig filters. In your case you can fetch the first item from an array with [0] and then slice off that same entry from the start of the same array with |slice(1) like this: {% paginate craft....


3

Yes, the post you linked to is what you need to do. I don't know where your settings variable is set so you may need to adjust a bit but something like this should get you started: {# Start your element query #} {% set query = craft .entries() .section('purchases') %} {# Check if settings.userId, if so, apply it to the query #} {% if settings.userId ...


3

You need to use newParentId: $navigationItem = new Entry(); $navigationItem->typeId = 4; $navigationItem->sectionId = $navigationSection->id; $navigationItem->title = $entry->title; $navigationItem->newParentId = $herbsNavigationEntry->id; $navigationItem->setFieldValue('navLink', '/tags/' . $entry->slug); if (!Craft::$app->...


3

As of Craft 3, there is no limit by default. Per the CHANGELOG: Element queries are no longer limited to 100 results by default. (Credit to Ben Croker who originally added this answer as a comment above. Adding this here as it would be better as the upvoted answer.)


3

I agree that any solution where you run a query and then conditionally render the result isn't very efficient. You can do this in SQL by using the where() condition. craft.entries .section('mySection') .where('content.field_field1 > content.field_field2') .all() Documentation on the where() condition can be found here.


3

You can configure the columns to display using the little cogwheel icon at the bottom (below the list of sections): This will open a modal window allowing you to select the fields you want to display in the table for each section. You can also rearrange the fields using drag-and-drop with the little handle icon to the left of the field name. As a sidenote, ...


2

Stumbled upon this looking to answer similar problem. You should be able to do this now without a plugin, using the multisort filter where you iterate through your profiles, e.g.: {% for profile in profiles|multisort(['profileLastname'],[SORT_ASC],[SORT_REGULAR]) %}


2

Answer update for Craft 3, and also adding in a secondary orderBy so that remaining entries are still ordered by date: {% set entries = craft.entries.section('news').orderBy('myLightswitch desc, postDate desc').all() %} Without the secondary orderBy, I have no idea what it's using for the remainder sort order but it's not postDate in my testing at least!


2

Thanks for this @mmc501 I have adapted the code to work the wonderful sprig plugin. {% set time = time ?? "now" %} <div class="grid text-center"> <div class="col"> <button sprig s-vars="time: '{{time|date_modify('-1 month')|date()}}'">Prev</button> </div> <div class=...


2

For Craft 3, after you get the ids you don't want (that would be an array), create your query to disregard those ids in the following way: {# omitIds = array of ids you want ommited #} {% set dontNeedIds = 'elements.id not in ('~omitIds|join(",")~')' %} {% set news = craft.entries.section("news").where(dontNeedIds).all() %}


2

Ok so here's how I ended up doing this. Essentially querying everything and adding a sourceId and targetId property to each result. In order to get this query to work though, we need to add a behavior to the Entry component to support two new properties, sourceId and targetId. // This basically left-joins the the relationship table to our sections. This ...


2

Assuming you can have multiple person blocks per entry (group), you could: Fetch your entries and eager-load the blocks and their entry to save some overheads {% set group = craft .entries() .id([100,200,300]) .with(['people.person:entry']) .all() %} Then build an array with each person.entry {# The array to store your person.entry #} {% set ...


2

You could do this: {# Your date 2020-11 in atom format e.g 2020-11-01T00:00:00+01:00 #} {% set departureDate = craft.app.request.getParam('departure') | date("Y-m") | atom %} {# Your date + 1 month in atom format e.g 2020-12-01T00:00:00+01:00 #} {% set untilDate = departureDate | date_modify('+1 month') | atom %} {# Query between those 2 dates #} ...


2

Doing further research i noticed the performance impact is mainly due to sorting on the default publication date. When sorting on the title field all the performance issues are gone.


2

I was thinking about this myself, and I found the following 2 articles which I have been going over. It looks like it is quite possible: http://craftsnippets.com/articles/using-template-hooks-in-craft-cms https://docs.solspace.com/craft/freeform/v3/overview/relations.html#overview The 2nd link has a heading for “Relating more than one element” which looks ...


2

Your code should work. Make sure that your Settings → Fields → Navigation has Use this field’s values as search keywords checked. Once you're sure it is checked, run ./craft resave/entries --update-search-index and it should start working. Note that instead of searching, you could use: {% set nav = craft.entries() .section('pages') .navigation('...


2

What an idiot. I almost never use Advanced element queries which is why it never popped into my head I guess. So you can add where() and orWhere() into your standard craft.entries calls. So my code becomes {% if craft.app.request.getParam('departure') | length %} {% for dept in craft.app.request.getParam('departure') %} {% set lastDay = dept [0:7]...


2

My approach: Create one channel for events with different entry types (solo-events, conference-events) Create one channel for conferences Re-use the date fields in both channels That way you can fetch and order events by the re-used date fields from both channels limiting to the entry types of solo-events and conferences with one query. That makes listings, ...


2

The equivalent SQL would be something like SELECT * FROM players ORDER BY teamName IS NOT NULL DESC, postDate DESC; but I'm not sure how that would translate in a simple element query. It'd be something like: {% set players = craft.entries.section('players').orderBy('teamName IS NOT NULL DESC, postDate desc').all() %}


2

I'm not aware of any straightforward way to remove that field from the Entry form. You might want to consider creating a small site module, adding some logic in an event handler to re-write/remove the contents of the expiration date as necessary?


2

Currently, it can't be done out of the box, since it's currently not possible to search the related entries like this. However, you can extend GraphQL to pull this off. Ideally, it would be possible to add arguments to existing queries, but that won't be possible until Craft 4.0, so you'll have to add an entirely new query. So, add a custom query and ...


2

You need to use the machine name of the section, not the single's slug. Check Settings -> Sections in the backend to find out the machine name. Machine names can't contain hyphens, so about-us isn't a valid machine name (though it is a valid slug). Maybe you're just mixing up about_us with about-us.


2

This answer is preliminary – see my comment asking for clarification. It really depends on what you want. Do you want the product page … … not to be available in Google's index any more? … not to be discoverable on the website (e.g. through a product search page)? … not to be accessible by direct URL? If the answer is yes to all three, simply delete the ...


1

There is no definite answer to your question but I think you can get pretty much what you want using just two types of categories. Section setup create 2 category groups: device types (with the deviceTypes) post types (with the postTypes) create an Articles section (with the article handle) configure you section with the following Entry URI format: {...


1

I believe you're looking for this instead. Event::on( Entry::class, Element::EVENT_AFTER_SAVE, static function (ModelEvent $event) { $entry = $event->sender; if ($entry->status === 'disabled' && $entry->propagating && $entry->resaving && ElementHelper::...


1

Prepending the field name with field: should do the trick. <?php return [ [ 'sectionId' => $this->sectionIds['locations'], 'typeId' => $this->typeIds['locations']['locations'], 'title' => 'Location 1', 'field:gmbId' => '123', ], ]; See https://github.com/craftcms/cms/blob/develop/tests/fixtures/...


1

I just realized that I can also use the Craft UI element in the field layout designer to accomplish this. http://craftsnippets.com/articles/using-template-ui-elements-to-extend-craft-cms-control-panel


Only top voted, non community-wiki answers of a minimum length are eligible