I have a Basic Page node whose body is using the PHP filter. On this page I am running the following PHP code to execute a SQL call against the website's database:

function get_latest_item_kid() {
    return db_query("SELECT kid FROM {table_name} ORDER BY title DESC")->fetchField();

What this should do is return the latest item's kid when the page loads. If more items get added, when the page refreshes this query will be run again and should return the newest item's kid.

This works the first time you load the page. However, no matter how many items are added, when you refresh the page the result of that query never changes. The only way to get this to update is by clearing the cache.

I have tried several techniques to disable various kinds of caching (assuming that's what my problem is), from clearing the page cache programmatically (didn't help) to adding SQL_NO_CACHE to the query (which caused the page not to load).

I'm using Drupal 8.

Any ideas?

  • 1
    Flushing Drupal caches shouldn't touch MySQL caches so you can probably rule that out. Using the PHP filter is known to screw with (and get screwed by) caching so replacing that with a preprocess function would be a good idea to start with. If you're still having problems, you're probably looking at a full-page caching issue, and it would be useful to know what version of Drupal you're using to be able to give the right advice – Clive May 19 '17 at 19:21
  • I'm using Drupal 8. I'm not totally sure what you mean by preprocess function, and Google isn't proving particularly helpful. – Ingulit May 22 '17 at 17:00

Your Answer

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

Browse other questions tagged or ask your own question.