Cheatsheets / WordPress

WordPress Cheatsheet

Complete WordPress reference. Hit Ctrl+P to print.

WP-CLI

wp core downloadDownload WordPress core files
wp core install --url=... --title=... --admin_user=... --admin_email=...Install WordPress
wp core updateUpdate WordPress core
wp plugin install akismet --activateInstall and activate a plugin
wp plugin update --allUpdate all plugins
wp plugin listList all installed plugins
wp theme activate twentytwentyfourActivate a theme
wp user create bob bob@example.com --role=editorCreate a new user
wp user listList all users
wp post create --post_title="Hello" --post_status=publishCreate a post
wp post list --post_type=post --post_status=publishList published posts
wp option get siteurlGet a site option
wp option update blogname "My Site"Set a site option
wp cache flushFlush the object cache
wp db export backup.sqlExport the database
wp db import backup.sqlImport a database dump
wp search-replace "old.com" "new.com" --all-tablesSearch and replace in database
wp cron event run --due-nowRun all due cron events
wp eval "echo get_bloginfo();"Run arbitrary PHP in WP context

The Loop

if (have_posts()) : while (have_posts()) : the_post(); ... endwhile; endif;Standard Loop structure
the_title()Output current post title
the_content()Output current post content
the_excerpt()Output current post excerpt
the_permalink()Output current post URL
the_date() / the_time("Y-m-d")Output post date/time
the_author()Output post author display name
the_post_thumbnail("large")Output featured image at size
the_ID()Output current post ID
get_the_title($post_id)Get title as string (no echo)
get_permalink($post_id)Get permalink as string
get_the_post_thumbnail_url($post_id, "medium")Get thumbnail URL as string

WP_Query

$q = new WP_Query(["post_type" => "post", "posts_per_page" => 10])Basic query
"post_type" => ["post", "product"]Multiple post types
"post_status" => "publish"Filter by status
"category_name" => "news"Filter by category slug
"tag" => "featured"Filter by tag slug
"author" => 5Filter by author ID
"s" => "search term"Full-text search
"orderby" => "date", "order" => "DESC"Sort by field and direction
"meta_key" => "price", "meta_value" => "100", "meta_compare" => "<"Meta query filter
"tax_query" => [["taxonomy" => "genre", "terms" => ["fiction"], "field" => "slug"]]Taxonomy query
"paged" => get_query_var("paged") ?: 1Pagination parameter
wp_reset_postdata()Reset global $post after custom query
get_posts(["numberposts" => 5, "post_type" => "post"])Simpler alternative - returns array

Actions & Filters (Hooks)

add_action("init", "my_function")Register action hook
add_action("init", "my_function", 20, 2)Register with priority and accepted args
remove_action("init", "my_function")Remove a registered action
do_action("my_custom_hook", $arg1)Fire a custom action
add_filter("the_title", "my_filter_function")Register filter hook
function my_filter($value) { return $value . " modified"; }Filter function must return modified value
remove_filter("the_title", "my_filter_function")Remove a registered filter
apply_filters("my_filter", $value)Apply a filter and get result
"wp_enqueue_scripts"Action: enqueue front-end scripts/styles
"admin_enqueue_scripts"Action: enqueue admin scripts/styles
"wp_head" / "wp_footer"Actions: inject HTML into head/footer
"save_post" / "delete_post"Actions: fired on post save/delete
"the_content" / "the_title"Filters: modify post content/title output

Custom Post Types & Taxonomies

register_post_type("product", $args)Register a custom post type
"labels" => ["name" => "Products", "singular_name" => "Product"]CPT labels array
"public" => true, "has_archive" => true, "supports" => ["title", "editor", "thumbnail"]Common CPT args
"show_in_rest" => trueEnable REST API support for CPT
"rewrite" => ["slug" => "products"]Custom URL slug for CPT
register_taxonomy("genre", "book", $args)Register custom taxonomy for a post type
"hierarchical" => trueCategory-like (true) vs tag-like (false) taxonomy
add_post_meta($post_id, "price", 29.99)Add post meta value
update_post_meta($post_id, "price", 39.99)Update post meta value
get_post_meta($post_id, "price", true)Get single meta value
delete_post_meta($post_id, "price")Delete post meta

Template Hierarchy

single-{post_type}.php → single.php → singular.php → index.phpSingle post type priority
page-{slug}.php → page-{id}.php → page.php → index.phpPage template priority
archive-{post_type}.php → archive.php → index.phpArchive template priority
taxonomy-{tax}-{term}.php → taxonomy-{tax}.php → taxonomy.php → index.phpTaxonomy archive priority
category-{slug}.php → category-{id}.php → category.php → archive.phpCategory archive priority
search.php → index.phpSearch results template
404.php → index.php404 error template
front-page.php → home.php → index.phpFront page template priority
get_template_part("template-parts/content", get_post_type())Load partial template
locate_template(["custom.php", "fallback.php"], true)Load first found template

Scripts & Styles

wp_enqueue_style("my-style", get_stylesheet_uri())Enqueue main stylesheet
wp_enqueue_style("my-style", get_template_directory_uri() . "/css/app.css", [], "1.0.0")Enqueue CSS with version
wp_enqueue_script("my-script", get_template_directory_uri() . "/js/app.js", ["jquery"], "1.0.0", true)Enqueue JS (true = in footer)
wp_localize_script("my-script", "myData", ["ajaxUrl" => admin_url("admin-ajax.php")])Pass PHP data to JavaScript
wp_register_style("handle", $src)Register without enqueuing
wp_dequeue_script("jquery")Remove a script from the queue

Options & Transients

get_option("blogname")Get site option
update_option("my_setting", $value)Save site option
delete_option("my_setting")Delete site option
add_option("my_setting", $default)Add option (skips if already exists)
set_transient("my_data", $value, HOUR_IN_SECONDS)Cache data with expiry
get_transient("my_data")Get cached value (false if expired)
delete_transient("my_data")Delete cached transient
DAY_IN_SECONDS / WEEK_IN_SECONDS / MONTH_IN_SECONDSTime constants