WP-CLI
wp core downloadDownload WordPress core fileswp core install --url=... --title=... --admin_user=... --admin_email=...Install WordPresswp core updateUpdate WordPress corewp plugin install akismet --activateInstall and activate a pluginwp plugin update --allUpdate all pluginswp plugin listList all installed pluginswp theme activate twentytwentyfourActivate a themewp user create bob bob@example.com --role=editorCreate a new userwp user listList all userswp post create --post_title="Hello" --post_status=publishCreate a postwp post list --post_type=post --post_status=publishList published postswp option get siteurlGet a site optionwp option update blogname "My Site"Set a site optionwp cache flushFlush the object cachewp db export backup.sqlExport the databasewp db import backup.sqlImport a database dumpwp search-replace "old.com" "new.com" --all-tablesSearch and replace in databasewp cron event run --due-nowRun all due cron eventswp eval "echo get_bloginfo();"Run arbitrary PHP in WP contextThe Loop
if (have_posts()) : while (have_posts()) : the_post(); ... endwhile; endif;Standard Loop structurethe_title()Output current post titlethe_content()Output current post contentthe_excerpt()Output current post excerptthe_permalink()Output current post URLthe_date() / the_time("Y-m-d")Output post date/timethe_author()Output post author display namethe_post_thumbnail("large")Output featured image at sizethe_ID()Output current post IDget_the_title($post_id)Get title as string (no echo)get_permalink($post_id)Get permalink as stringget_the_post_thumbnail_url($post_id, "medium")Get thumbnail URL as stringWP_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 parameterwp_reset_postdata()Reset global $post after custom queryget_posts(["numberposts" => 5, "post_type" => "post"])Simpler alternative - returns arrayActions & Filters (Hooks)
add_action("init", "my_function")Register action hookadd_action("init", "my_function", 20, 2)Register with priority and accepted argsremove_action("init", "my_function")Remove a registered actiondo_action("my_custom_hook", $arg1)Fire a custom actionadd_filter("the_title", "my_filter_function")Register filter hookfunction my_filter($value) { return $value . " modified"; }Filter function must return modified valueremove_filter("the_title", "my_filter_function")Remove a registered filterapply_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 outputCustom 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 CPTregister_taxonomy("genre", "book", $args)Register custom taxonomy for a post type"hierarchical" => trueCategory-like (true) vs tag-like (false) taxonomyadd_post_meta($post_id, "price", 29.99)Add post meta valueupdate_post_meta($post_id, "price", 39.99)Update post meta valueget_post_meta($post_id, "price", true)Get single meta valuedelete_post_meta($post_id, "price")Delete post metaTemplate Hierarchy
single-{post_type}.php → single.php → singular.php → index.phpSingle post type prioritypage-{slug}.php → page-{id}.php → page.php → index.phpPage template priorityarchive-{post_type}.php → archive.php → index.phpArchive template prioritytaxonomy-{tax}-{term}.php → taxonomy-{tax}.php → taxonomy.php → index.phpTaxonomy archive prioritycategory-{slug}.php → category-{id}.php → category.php → archive.phpCategory archive prioritysearch.php → index.phpSearch results template404.php → index.php404 error templatefront-page.php → home.php → index.phpFront page template priorityget_template_part("template-parts/content", get_post_type())Load partial templatelocate_template(["custom.php", "fallback.php"], true)Load first found templateScripts & Styles
wp_enqueue_style("my-style", get_stylesheet_uri())Enqueue main stylesheetwp_enqueue_style("my-style", get_template_directory_uri() . "/css/app.css", [], "1.0.0")Enqueue CSS with versionwp_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 JavaScriptwp_register_style("handle", $src)Register without enqueuingwp_dequeue_script("jquery")Remove a script from the queueOptions & Transients
get_option("blogname")Get site optionupdate_option("my_setting", $value)Save site optiondelete_option("my_setting")Delete site optionadd_option("my_setting", $default)Add option (skips if already exists)set_transient("my_data", $value, HOUR_IN_SECONDS)Cache data with expiryget_transient("my_data")Get cached value (false if expired)delete_transient("my_data")Delete cached transientDAY_IN_SECONDS / WEEK_IN_SECONDS / MONTH_IN_SECONDSTime constants