Skip to content

Performance tips

Mike Hermans edited this page Oct 19, 2020 · 1 revision

We all love websites that run fast, so here are some tips to keep in mind when building a website with Clarkson Core.

Use self::get_many() instead of new WP_Query

The self::get_many() methods and sets no_found_rows to true to disable pagination.

A new WP_Query object runs five queries by default, including calculating pagination and priming the term and meta caches. Each of the following arguments will remove a query:

  • 'no_found_rows' => true: useful when pagination is not needed.
  • 'update_post_meta_cache' => false: useful when post meta will not be utilized.
  • 'update_post_term_cache' => false: useful when taxonomy terms will not be utilized.

Finally it returns an Array of the same WordPress Object Class (hence the self).

Source: 10up

PHP render_twig vs. Twig include

Using clarkson_template::render_twig in a PHP foreach is a lot more expensive than including a partial in a twig template, which is the most common use-case.

class Post {
    public static function get_recently_published() {
      return self::get_many( array( 'posts_per_page' => 10 ) );
    }
}
{% for item in object.get_recently_published() %}
  {% include 'teaser.twig' with { 'post': item } %}
{% endfor %}

Related GitHub thread