Intermediatenote 03 of 1603 reasons · 03 notes
WordPressIntermediate
Cache expensive queries
Use transients for custom queries that do not need to hit the database every request.
Code example
Copy, adapt, and ship.
Read the numbered lines, copy the complete snippet, then adjust the values for your project.
$cache_key = 'theme_popular_posts';$posts = get_transient( $cache_key ); if ( false === $posts ) { $posts = new WP_Query([ 'posts_per_page' => 6, 'meta_key' => 'views', 'orderby' => 'meta_value_num', ]); set_transient( $cache_key, $posts, HOUR_IN_SECONDS );}Why this matters
The production reason.
- 01
Repeated custom queries can hit the database on every page view, even when the result changes only a few times per day.
- 02
Transients let you cache the computed result and refresh it on a schedule or when content changes.
- 03
Use this for popular posts, related products, taxonomy-heavy lists, and dashboard summaries.
How it holds up outside a demo.
- WordPressClear transients when the related content is updated.
- WooCommerceCache product lookups, but avoid caching user-specific cart data.
- Managed hostingPair transients with object cache when Redis is available.
Explore other stacks
Jump into a different technology.
16 WordPress notes on this shelf, and six more stacks beside it.
Use next/image for image optimization
Next.js image optimization starts with next/image sizing, lazy loading, modern formats, and predictable dimensions.
Open the noteKeep state close to the interaction
Local state avoids unnecessary parent renders and keeps component intent obvious.
Open the noteAbort stale fetch requests
Cancel outdated browser requests so fast UI changes do not render old data.
Open the noteBuild a CSS gradient generator with code export
A CSS gradient generator should preview color stops, preserve accessible contrast, and export clean CSS code.
Open the noteReduce LCP with image optimization
Reduce LCP with image optimization by resizing, converting, and compressing images before they reach production.
Open the noteUse real buttons for actions
If it changes state, use button. If it navigates, use a link.
Open the note