Slide 1

Slide 1 text

Creating efficient WordPress Queries with WP_Query - By Krishna Neupane - @krishneup 1

Slide 2

Slide 2 text

Introduction ● Enable us to perform complex database queries in a safe, simple and modular manner. 2

Slide 3

Slide 3 text

Introduction ● Enable us to perform complex database queries in a safe, simple and modular manner. ● Can be created by initiating WordPress Query class 3

Slide 4

Slide 4 text

Introduction ● Enable us to perform complex database queries in a safe, simple and modular manner. ● Can be created by initiating WordPress Query class ● Has properties & methods 4

Slide 5

Slide 5 text

Introduction ● Enable us to perform complex database queries in a safe, simple and modular manner. ● Can be created by initiating WordPress Query class ● Has properties & methods ● Offers large number of parameter options to perform query. 5

Slide 6

Slide 6 text

6 Let’s see an example

Slide 7

Slide 7 text

7 Can we improve this?

Slide 8

Slide 8 text

Only run the query you need, But how? 8

Slide 9

Slide 9 text

WP_Query makes following queries by default ● Calculates pagination 9

Slide 10

Slide 10 text

WP_Query makes following queries by default ● Calculates pagination ● Queries post meta and taxonomy terms 10

Slide 11

Slide 11 text

WP_Query makes following queries by default ● Calculates pagination ● Queries post meta and taxonomy terms ● Calculates results 11

Slide 12

Slide 12 text

We can improve by passing some parameters & techniques 12

Slide 13

Slide 13 text

When you need to retreive only last n posts, without pagination, it is useful to use undocumented (it is documented only on code) parameter ‘no_found_rows’ => true. This will increase performance. 13 When pagination is not needed

Slide 14

Slide 14 text

This is helpful when you need to show Posts without adding post meta information to the cache. This helps in performance. 14 Useful when post meta info will not be utilized.

Slide 15

Slide 15 text

This is helpful when you need to show Posts without adding post term information to the cache. This helps in performance too. 15 Useful when taxonomy terms will not be utilized.

Slide 16

Slide 16 text

Note: If a persistent object cache backend (such as memcached) is used, these caching parameter flags are set to false by default since there is no need to update the cache every page load when a persistent cache exists. 16

Slide 17

Slide 17 text

This is useful when only the post IDs are needed. 17 Specify return field parameters

Slide 18

Slide 18 text

Do not use posts_per_page => -1 What if we have 100,000 posts? This could crash the site. 18

Slide 19

Slide 19 text

Avoid post__not_in Helpful but can lead to poor performance on busy and large websites. Link - https://docs.wpvip.com/technical-references/code-quality-and-best-pract ices/using-post__not_in/ 19

Slide 20

Slide 20 text

20 What’s the alternative techniques for post__not_in ?

Slide 21

Slide 21 text

21 Let’s see it

Slide 22

Slide 22 text

Do not use $wpdb & get_posts() - unless you have a solid reason 22

Slide 23

Slide 23 text

Detect slow queries with Query Monitor plugin 23

Slide 24

Slide 24 text

Utilize Transients feature when possible 24

Slide 25

Slide 25 text

25 Provide a way to override. Your developer users will thank you.

Slide 26

Slide 26 text

Using WP_Query for the search & filters 26

Slide 27

Slide 27 text

27 Ways to pass search parameter for text search

Slide 28

Slide 28 text

But, wait, there’s a problem By default, the default search engine for WordPress searches only the title and post_content. 28

Slide 29

Slide 29 text

29 Use Relevanssi plugin integration

Slide 30

Slide 30 text

30 Supercharge further adding filters to the WP_Query My favourite combination : Search & Filter Pro + Relevanssi Relevanssi - https://wordpress.org/plugins/relevanssi/ Search & Filter - https://wordpress.org/plugins/search-filter/

Slide 31

Slide 31 text

WordPress 6.1 has WP_Query improvements 31 If the same database query is run more than once, the result will be loaded from cache.

Slide 32

Slide 32 text

Final point Happy engineering ! 32

Slide 33

Slide 33 text

33 Reach me via Website: www.krishneup.com twitter: @krishneup Email: [email protected]