Hot answers tagged

9

This requires a bit of extra work on the ElementQuery, but it can be efficiently done in the same query. We first perform an inner join on the author's content table using the authorId so that the custom field is available to us. in Twig: {% set elementQuery = craft.entries() .section('sectionHandle') .with(['author']) .innerJoin('{{%content}} ...


6

This is a great chance to use the group filter. First, get all your matrix blocks as an array: {% set talkBlocks = entry.talks.find() %} Then group them by type: {% set talksByCategories = talkBlocks|group('type') %} At this point, you have an associative array where the keys are the block types, and the values are arrays of the blocks with that type. ...


6

There's a positionedBefore parameter for craft.entries. Use this with a criteria model filtered to show the entries you are interested in only. {% set parent = entry.getParent() %} {% set siblingsIds = parent.getChildren().ids() %} {% set positionedBefore = craft.entries({ id: siblingsIds, order: 'lft desc', positionedBefore: entry, }).total() %...


6

See What is getContent() / the ContentModel, and how do I use it to get and set custom field values? So you needed to change the method used for setting content on an element. Also see In a plugin, how do I save an entry with a related entry So the relation fields always expect an array of ids, even if there is a single relation. Putting those together ...


5

Close! This should do it: {% set products = craft.commerce.products({ relatedTo: (category), order: 'defaultPrice', }) %} With the quotes it's a string. Without the quotes, Twig is expecting a variable you've defined called defaultPrice that doesn't exist.


5

The probably most easy way to do this is to sort the grouped array by key, which unfortunatelly is only possible with a plugin, as Twig's sort filter only allows to sort by value. Here I'm using Michael Rog's Supersort, but there are also other plugins that do this. {% set allProfiles = craft.entries.section('members').order('profileLastname').limit(null) %...


4

Craft always saves a complete DateTime value to the database, even if you only make it set / show the time part of it in the Control Panel. To ignore the date part (which in this case is set to the day you saved your entry) within the order parameter, you can make use of the time() MySQL function: {% set entries = craft.entries.section('events').order('...


4

Another option is to use the Preparse plugin to automatically parse out and store last names in a hidden field. Install the Preparse plugin Create a new “Last Name” field using the Preparse field type Set the field’s Twig template setting to this: {{ entry.title|split(' ')|last }} Edit the People section’s entry type settings, and add the Last Name field ...


4

I was close. I was correct in assuming my syntax was incorrect for setting the value of an entries field. Since an entries field is an array of elements, not just a single element, I just needed to set the value of the field as an array instead of a string. Changing $cart->setContentFromPost(['entriesFieldHandle' => $entry->id]); to $cart->...


4

Since the templates are rendered on the server and not in a regular http request (in the front end templates), the globals are not loaded automatically, into the global twig namespace. You can still access your global set and use it as you usually would by setting it to a variable. You can do so like this: {% set companyInfo = craft.globals.getSetByHandle(...


4

The way to do this is with the group filter. You want to group the entries by the first character of the title. Get the entries as an actual array {% set allEntries = craft.entries.section('artists').order('title').find %} Group them by first letter of title. first will return the first character of a string. upper converts a string to uppercase. {% set ...


3

I went and looked at the MatrixBlockModel; there's an attribute named sortOrder (If you output {{sortOrder}} inside the for loop, it will display the index of that block). You can use that attribute to sort by, in reverse: {% for cta in entry.ctas.order('sortOrder desc') %}


3

The trouble you are running into is trying to add a string value to an integer to get your total. You have to use order.totalPrice without the currency filter to be able to add all the values together. {% set cart = craft.commerce.cart %} {% set totalOrders = 0 %} {% for order in craft.commerce.orders.customer( craft.commerce.customer ).find() %} {% set ...


3

There's this super useful SuperSort plugin that would make this possible. {% set sortAsTemplate = '{{ object.section == "Exhibitions" ? object.startDate|date("U") : object.date|date("U") }}' %} {% set sortedOccurrences = allOccurrences|supersort('sortAs', sortAsTemplate) %}


3

Try something slightly different in concept... On your Products template - /products/index.html - start with your Authors first and then look for the Products which are related to them. Hopefully this will help you make sense of it... {# find all author entries #} {% set authors = craft.entries.section('authors').order('title') %} <ul> {# loop ...


3

It’s not possible to do that unless you’ve already got the last word saved somewhere. Here’s what I would recommend: Create two new Plain Text fields - “First Name” and “Last Name” Go to your People section’s entry type settings Uncheck the “Show the Title field?” setting and set the “Title Format” setting to: {firstName} {lastName} Add the “First Name” ...


3

Just setting the limit property doesn’t do the trick alone, you need to deactivate pagination, too: 'criteria' => [ 'section' => 'trucks', 'limit' => 10, 'orderBy' => 'title asc', ], 'paginate' => false,


3

According to SUBSTRING, the first character starts at 1. So if you wanted to start at the position immediate after The (with one space), you'd need to start at position 5. Just did a test locally and it works as expected for me if I use: .order('(CASE WHEN title LIKE "The %" THEN SUBSTRING(title, 5) ELSE title END ) asc')


2

I don't think it's possible to order by the name or handle of Entry Types. To order by the ID of your Entry Types use 'typeId' as the value: {% set entries = craft.entries.section('more-info').order('typeId') %} Now you only have to create your Entry Types alphabetically ;)


2

Using the group filter you will always end up with the sort order of your entries. So you would need to sort the entries by your categories field, but this probably doesn't work. But you can do the following: {# Get the categories related to my "entrySection" entries in custom order #} {% set entries = craft.entries.section('entrySection') %} {% set ...


2

This will now work as of Sprout Forms v2.2.0: {# Ordering #} {% for formEntry in craft.sproutForms.entries.formHandle('contact').order('address desc') %} ... {% endfor %} {# Filtering #} {% for formEntry in craft.sproutForms.entries.formHandle('contact').firstName('Zoe') %} ... {% endfor %#}


2

It seams, that the columns for the form values are not part of the sql, that's why it never will work like this. Maybe it works in upcomming versions of the Sprout Forms plugin. As a workaround you can use the SuperSort Plugin. With this plugin it's possible to sort your form entries like you need it. Step 1 -> Download and Install SuperSort Plugin -> ...


2

The craft_structureelements table contains the data you need to determine the exact position & level of each entry in your structure. But you'd need some fancy plugin logic to attack that data and use it to your advantage. In all honesty, I really feel like you're going the wrong way on this, and you're better served trying to find a way to make the ...


2

This is only possible with the id parameter in combination with fixedOrder. {% set entries = craft.entries.id([307, 287, 488]).fixedOrder(true) %} You can look up your singles' IDs in the URL of the entries in the CP. example.com/admin/entries/section1/307-section1/


2

I just noticed that you've posted an answer to your question already. Your approach using the group filter is probably all you need, as the allDesigns array is sorted by postDate desc and by using the group filter you'll keep that sort order, your author IDs and the nested entries will both be sorted by the design post date. To query the user models in the ...


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

The Order History is the history of that particular order and not designed to pull in other prior orders from that customer. You might want to make a feature request if you want to be able to see customer histories in the order view: https://github.com/craftcms/commerce/issues ....if they're a registered Craft user you can use the email at the top to get ...


2

This should do the trick: 'criteria' => [ 'section' => 'trucks', 'limit' => 10, 'order' => 'title asc', ] The Element Criteria Model documentation has more information about setting criteria. Update for Craft 3.x* The “order” element query param has been deprecated. Use “orderBy” instead. * Craft v3.0.11 reports this in the /...


2

I did something like this as a workaround: Loop over entries, fetching all data you need and adding it to an array. Sorting that array with SuperSort and than displaying all info by looping over the array. {% set tourDates = [] %} {% set artists = craft.entries.section('artists') %} {% for artist in artists %} {% if artist.tourDates %} {% for ...


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