Build classic and block WordPress themes from scratch: the template hierarchy, The Loop, the_title() and the_content(), template parts, menus, the Customizer, child themes and theme.json.
Lessons
WP_Query is the class WordPress uses to fetch posts, and creating your own instance lets a template show an extra list — related posts, a featured carousel, the latest three projects — without disturbing the page's main query.
<?php
$latest = new WP_Query(
array(
'post_type' => 'post',
'posts_per_page' => 3,
'ignore_sticky_posts' => true,
'no_found_rows' => true, // We are not paginating, so skip the count query.
)
);
?>
<?php if ( $latest->have_posts() ) : ?>
<section class="latest-posts">
<h2><?php esc_html_e( 'Latest posts', 'gdc-starter' ); ?></h2>
<?php while ( $latest->have_posts() ) : $latest->the_post(); ?>
<?php get_template_part( 'template-parts/content', 'card' ); ?>
<?php endwhile; ?>
</section>
<?php endif; ?>
<?php wp_reset_postdata(); ?>Note $latest->have_posts() and $latest->the_post() — you call them on your object, not the bare functions that read the main query.
$latest->the_post() overwrites the global $post. After your loop, every template tag would still describe the last post from your custom list. wp_reset_postdata() restores the global $post to the main query's current post. Leave it out and the comments below your related-posts box belong to the wrong post.
| Argument | Example | Meaning |
|---|---|---|
post_type | 'book' or array( 'post', 'page' ) | What to fetch |
posts_per_page | 6 (-1 for all — avoid on large sites) | How many |
post__not_in | array( get_the_ID() ) | Exclude posts |
category_name | 'news' | By category slug |
tax_query | See below | By any taxonomy |
meta_query | See below | By custom field |
orderby / order | 'date', 'title', 'rand' / 'DESC' | Sorting |
no_found_rows | true | Skip counting total rows — faster when not paginating |
<?php
$categories = wp_get_post_categories( get_the_ID() );
if ( $categories ) {
$related = new WP_Query(
array(
'category__in' => $categories,
'post__not_in' => array( get_the_ID() ),
'posts_per_page' => 3,
'ignore_sticky_posts' => true,
'no_found_rows' => true,
)
);
if ( $related->have_posts() ) {
echo '<aside class="related"><h2>' . esc_html__( 'Related posts', 'gdc-starter' ) . '</h2>';
while ( $related->have_posts() ) {
$related->the_post();
get_template_part( 'template-parts/content', 'card' );
}
echo '</aside>';
}
wp_reset_postdata();
}<?php
$fantasy_bestsellers = new WP_Query(
array(
'post_type' => 'book',
'posts_per_page' => 8,
'tax_query' => array(
array(
'taxonomy' => 'genre',
'field' => 'slug',
'terms' => 'fantasy',
),
),
'meta_query' => array(
array(
'key' => '_gdc_bestseller',
'value' => '1',
'compare' => '=',
),
),
)
);Watch out: meta_query searches an unindexed column and gets slow on large sites. For data you filter by often, a taxonomy is usually the faster and cleaner choice.| You want to | Use |
|---|---|
| Change the posts on the main page (count, order, type) | pre_get_posts — never a new query in the template |
| A simple array of posts, no Loop needed | get_posts() — a thin wrapper that returns an array |
| Replace the main query | Nothing. Never use query_posts(). |
front-page.php with no_found_rows.single.php, excluding the current post.wp_reset_postdata(), reload a post with comments, and see the comments area break — then put it back.In accordance with Section 6 & Section 7 of the DPDP Act 2023, third-party analytics and marketing trackers are strictly gated behind your affirmative consent. You may withdraw consent at any time under Section 6(4).
Essential for session authentication (ci_session), Cross-Site Request Forgery security (csrf_cookie_name), and Cloudflare DDoS/bot prevention. Cannot be disabled.
Gated under DPDP Section 7. Allows Google Analytics 4 (Google Consent Mode v2) to measure aggregate, anonymized tool usage.
Enables browser local storage to preserve your theme selection (dark/light mode) and code editor layout between visits.
Used to display developer sponsorships and native tool partner notices.