Skip to main content
6 votes
Accepted

Maintaining order when getting by entries by ID

You can do this by adding .fixedOrder(true) to your query: {% set ids = ['4213','3200','4204','4212'].fixedOrder(true) %} https://docs.craftcms.com/v3/dev/element-queries/tag-queries.html#fixedorder
Jamie Wade's user avatar
  • 3,644
5 votes

Merging 2 Entries Array's

Just a few side notes: neither the first entries variable nor the second entries variable is an array - both are objects of kind craft\elements\db\EntryQuery (or craft\ElementCriteriaModel if it's ...
Robin Schambach's user avatar
5 votes
Accepted

Local DB SQL 'CDbException' Errors on Craft 2 site

this is a MySQL/i version issue on Craft 2 sites. You'll need to add this to your database config (craft/config/db.php): 'initSQLs' => array("SET SESSION sql_mode='STRICT_TRANS_TABLES,...
Cole Henley's user avatar
  • 1,781
4 votes
Accepted

Sending a custom email in the users prefered locale

You can translate string in PHP and Twig via translate function public static function t($message, $variables = array(), $source = null, $language = null, $category = 'craft') Twig {{ 'my string'|t(...
Robin Schambach's user avatar
4 votes
Accepted

How can I return only 1 random block in a Matrix in Craft using Twig?

You can create a random order by including rand() in your OrderBy function. {% set randomBlock = entry.carouselMatrix.orderBy('rand()').one() %} {% if randomBlock is not null %} {% set ...
Robin Schambach's user avatar
4 votes

Just upgraded to Craft 3, environmentVariables changed or stopped working

Environment variables don't exist in Craft 3 in the same way that they existed in Craft 2. Instead, they have been replaced with system environment variables and aliases. The Changes in Craft 3 docs ...
Steve Rowling's user avatar
3 votes
Accepted

How to directly get value of text field in a given section of type structure?

Try this: {% set value = craft.entries.section('mySectionHandle').type('entryTypeHandle').first().myTextFieldHandle %} {{ value }} Just with your field handle at the end
Barbara Cinotti's user avatar
3 votes
Accepted

Ajax Post to Plugin Controller

Based on your controller's class name, I believe you need to POST to this endpoint... /actions/googleMap/map/getLocations
Lindsey D's user avatar
  • 24.1k
3 votes

How to have Craft 3 and 2 on same host?

Both the craft/ folder name in Craft 2, and the craft executable in Craft 3, can be renamed. Since the craft executable is user-facing (when SSH’d in), I’d recommend leaving that alone and rename ...
Brandon Kelly's user avatar
3 votes

How to create an entry with MySQL?

If you really, really want to create an entry from within the database (you must have a VERY good reason to do so), then you should create a row in: entries elements elements_sites content If you'd ...
Paul's user avatar
  • 6,348
3 votes
Accepted

How to filter entries with null value in date field?

The special strings :empty: and :notempty: might be what you're looking for. The docs list it only against expiryDate, but I think it's available for any field: https://docs.craftcms.com/v3/dev/...
James Smith's user avatar
  • 5,689
3 votes
Accepted

German special characters like umlauts (ä, ö, ü) are not converted correctly with convertFilenamesToAscii

This was a bug and has now been fixed in Craft 3.5.12.1 https://github.com/craftcms/cms/issues/6923
Sanbo's user avatar
  • 129
3 votes

mkdir(): Permission denied

It's been a while since I was in Craft 2, but I'd suggest checking out this page in the docs: https://craftcms.com/docs/2.x/folder-structure.html Do all of the directories mentioned on that page exist ...
John Morton's user avatar
3 votes
Accepted

Upgrade from craft 2 fails due to read-only mode

The goal is to eventually upgrade to Craft 4.x, but I thought it would be easier to first update to Craft 3.x Not just easier; upgrading to the latest 3.x release is a prerequisite for upgrading to ...
Mats Mikkel Rummelhoff's user avatar
3 votes
Accepted

Craft CMS 2 - Exporting entity issue

That could be related to the version of PHP on your server being too new for the version of Craft you're using. I'm sure you're aware, but Craft 2 is no longer supported; I'd recommend updating to 3 (...
darylknight's user avatar
  • 3,290
3 votes

My site has been hacked. I have managed to clean it up. But now, most of my links are looping back to the home page

There was a bug in Craft 4 mentioning RCE vulnerabilities, and I saw the same results of hacking on my site(s), but I'm not sure about such an old version of Craft. Can you restore the site from a ...
4midori's user avatar
  • 668
2 votes

Programmatically Login From Plugin

If you want to login a user, you need to use the login function craft()->userSession->login($loginName, $password, $rememberMe))
Robin Schambach's user avatar
2 votes

Failed Stale Template Cache Delete Error...Analyzed...But No Answer

OK, I found the answer to this. Here is the correct answer. Basically change dateCreated to elements.dateCreated in my template.
hvs's user avatar
  • 315
2 votes
Accepted

Impossible to invoke a method ("limit") on an array

craft.entries now returns an ElementQuery. When you add the .all() you're telling Craft to execute the query, which returns an array of Elements. That means that solutions is now a simple array, and ...
andrew.welch's user avatar
  • 11.6k
2 votes
Accepted

Custom plugin log not logging

Put the force parameter to true MyPlugin::log("Errors:\n\n" . print_r($fieldsData, true), LogLevel::Error, true); Custom Log in Craft
Wasnu's user avatar
  • 36
2 votes
Accepted

Is it possible to pass a variable to an event in a plugin?

This will not work as the field value will be the new value in both events. You'll need to get the old value from the entry that exists in Craft before saving the new entry, and then perform the ...
Ben Croker's user avatar
  • 7,796
2 votes
Accepted

Adding entry type using setContentFromPost

Relationships are always added as arrays of IDs, even if you are only relating a single element: $user->setContentFromPost(array( 'companyName' => $admin->companyName, 'companyId' =&...
Mike Pepper's user avatar
  • 4,391
2 votes
Accepted

Variable "entry" does not exist in template at Structure Section

That entry variable will only be predefined for the template when you go to one of your entry’s URLs. Your section’s Entry URL Format for top-level entries is {slug}, and your entry’s slug is ...
Brandon Kelly's user avatar
2 votes
Accepted

Multiple and or relatedTo in plugin element criteria

Aren't you missing the ->find() on the last bit? $entries = craft()->elements->getCriteria(ElementType::Entry, [ 'section' => 'products', 'relatedTo' => ['and'...
Oli's user avatar
  • 7,515
2 votes
Accepted

Update a lightswitch field in Craft CMS 2

If it's only one single field and you want to bypass all Craft validations for required fields you can change the value directly in your database using Yii commands and the update function function ...
Robin Schambach's user avatar
2 votes

CSRF token is null or empty in craft 2

For Craft 2, these are the values you're looking for... window.csrfTokenName = "{{ craft.config.get('csrfTokenName') }}"; window.csrfTokenValue = "{{ craft.request.getCsrfToken }}"; They changed ...
Lindsey D's user avatar
  • 24.1k
2 votes

Clear template cache after update sale on Craft Commerce

More than likely the issue is not the prices not updating, but the sale conditions as Luke mentioned. In theory, Craft will automatically clear template caches when an element within the cached ...
Jeremy Daalder's user avatar
2 votes
Accepted

Pagination Slug Issue

Pagination is for breaking up a list of entries into different pages. p3 means page 3 of entries. What you're looking for is the ability to link to the next/previous entry. If you are ordering your ...
Simon Kuran's user avatar
  • 3,015
2 votes
Accepted

Craft 2 Commerce 1 cart session expiry

Create a commerce.php config file in your config folder and set the cartCookieDuration to a time interval. The default is 3 months. <?php return [ 'cartCookieDuration' => 'P3M', '...
Luke Holder's user avatar
  • 6,897
2 votes

Which twig file is being used and how?

You could use this variable to get the template name: {{ _self.getTemplateName() }} Put it at the top of all your template files: <!-- {{ _self.getTemplateName() }} --> And then for every ...
SamC's user avatar
  • 482

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