Classes

Query Classes
query
WP_Query
The most-used class in WP dev. Constructs and executes custom database queries for posts. Supports meta, taxonomy, date, and ordering parameters.
$q = new WP_Query([ ‘post_type’ => ‘project’, ‘posts_per_page’ => 6, ‘meta_key’ => ‘_featured’, ‘meta_value’ => ‘1’, ]); while ( $q->have_posts() ) { $q->the_post(); } wp_reset_postdata();
have_posts() the_post() get_posts() found_posts max_num_pages
query
WP_Term_Query
Dedicated class for querying taxonomy terms. More performant than get_terms() for complex conditions; supports meta queries and hierarchical filtering.
$tq = new WP_Term_Query([ ‘taxonomy’ => ‘category’, ‘hide_empty’ => false, ‘number’ => 10, ]); foreach ( $tq->get_terms() as $term ) { echo $term->name; }
get_terms() query()
query
WP_Comment_Query
Handles querying comments with full support for filtering by post, user, type, date, and meta fields.
$cq = new WP_Comment_Query([ ‘post_id’ => get_the_ID(), ‘status’ => ‘approve’, ‘number’ => 5, ]); foreach ( $cq->comments as $c ) { echo $c->comment_content; }
query() get_comments() get_comment_ids()
query
WP_User_Query
Queries users from the database. Supports role filtering, meta queries, ordering, and pagination — the proper way to fetch multiple users.
$uq = new WP_User_Query([ ‘role’ => ‘Editor’, ‘orderby’ => ‘registered’, ‘order’ => ‘DESC’, ]); foreach ( $uq->get_results() as $user ) { echo $user->display_name; }
get_results() get_total()
Post, Term & Taxonomy Objects
post
WP_Post
Represents a single post object. Every post retrieved from the database is hydrated into this class. Access properties directly.
$post = get_post( 42 ); // WP_Post object echo $post->post_title; echo $post->post_status; echo $post->post_type; echo $post->post_content;
post_title post_content post_status post_type post_author
taxonomy
WP_Term
Represents a taxonomy term (category, tag, or custom). Returned by get_term(), get_the_terms(), and WP_Term_Query.
$term = get_term( 5, ‘category’ ); // WP_Term object echo $term->name; echo $term->slug; echo $term->description; echo $term->count;
term_id name slug taxonomy parent count
taxonomy
WP_Taxonomy
Stores all registered data for a taxonomy. Retrieve via get_taxonomy(). Useful for reading labels, capabilities, and settings programmatically.
$tax = get_taxonomy( ‘category’ ); echo $tax->label; echo $tax->hierarchical ? ‘hier’ : ‘flat’; print_r( $tax->labels );
label labels hierarchical public capabilities
post
WP_Post_Type
Holds all the data for a registered post type. Returned by get_post_type_object(). Useful for inspecting labels, supports, and capabilities.
$pt = get_post_type_object( ‘post’ ); echo $pt->label; echo $pt->publicly_queryable; print_r( $pt->supports );
label supports capabilities rewrite
Users, Roles & Capabilities
user
WP_User
The user object class. Contains all user data including roles, capabilities, and meta. The definitive way to interact with a WP user programmatically.
$user = new WP_User( get_current_user_id() ); echo $user->user_email; echo $user->display_name; if ( $user->has_cap( ‘edit_posts’ ) ) { // can edit } $user->add_role( ‘contributor’ );
has_cap() add_role() remove_role() add_cap() get()
user
WP_Role
Represents a single user role and its capabilities. Use to add or remove capabilities from a specific role.
$role = get_role( ‘editor’ ); $role->add_cap( ‘manage_options’ ); $role->remove_cap( ‘delete_others_posts’ ); print_r( $role->capabilities );
add_cap() remove_cap() has_cap() capabilities
user
WP_Roles
Manages all registered roles globally. Retrieve the global instance via wp_roles(). Use to add or remove roles site-wide.
$roles = wp_roles(); $roles->add_role( ‘client’, ‘Client’, [ ‘read’ => true, ‘upload_files’ => true ] ); $roles->remove_role( ‘subscriber’ );
add_role() remove_role() get_role() get_names()
HTTP, Requests & Errors
http
WP_HTTP
The low-level class powering WordPress’s HTTP transport layer. Normally accessed through helper functions like wp_remote_get() rather than directly.
$response = wp_remote_get( ‘https://api.example.com/data’, [ ‘timeout’ => 10 ] ); $body = wp_remote_retrieve_body( $response ); $code = wp_remote_retrieve_response_code( $response );
request() get() post() head()
http
WP_REST_Request
Encapsulates an incoming REST API request. Access params, headers, body, and method inside REST API callback functions.
register_rest_route( ‘myplugin/v1’, ‘/items’, [ ‘methods’ => ‘GET’, ‘callback’ => function( WP_REST_Request $req ) { $id = $req->get_param( ‘id’ ); return rest_ensure_response( [ ‘id’ => $id ] ); }, ] );
get_param() get_params() get_header() get_method() get_body()
http
WP_REST_Response
Encapsulates a REST API response. Lets you set the data, status code, and headers before returning to the client.
$response = new WP_REST_Response( [ ‘message’ => ‘Created’ ], 201 ); $response->header( ‘X-Custom-Header’, ‘value’ ); return $response;
set_data() set_status() header() add_link()
error
WP_Error
Standard WP error handling object. Functions return WP_Error on failure — always check with is_wp_error() before using results.
$result = wp_insert_post( $data ); if ( is_wp_error( $result ) ) { echo $result->get_error_message(); } // Creating your own: return new WP_Error( ‘missing_field’, ‘The name field is required.’, [ ‘status’ => 400 ] );
get_error_message() get_error_code() get_error_data() add() merge_from()
Admin, Widgets & Utilities
utility
WP_Widget
Base class for building sidebar widgets. Extend it and implement widget(), form(), and update() to create a fully functional widget.
class My_Widget extends WP_Widget { public function __construct() { parent::__construct( ‘my_widget’, ‘My Widget’ ); } public function widget( $args, $inst ) { echo $args[‘before_widget’]; echo esc_html( $inst[‘title’] ?? ” ); echo $args[‘after_widget’]; } public function form( $instance ) { /* form */ } public function update( $new, $old ) { return $new; } }
widget() form() update()
utility
WP_List_Table
Abstract base for building admin list tables (like the Posts screen). Extend to create custom admin data tables with sorting, pagination, and bulk actions.
class My_Table extends WP_List_Table { public function get_columns() { return [ ‘cb’ => ‘<input type=”checkbox”>’, ‘title’ => ‘Title’, ‘date’ => ‘Date’, ]; } public function prepare_items() { /* … */ } }
get_columns() prepare_items() display() get_bulk_actions()
utility
WP_Customize_Manager
The Customizer API manager. Used in customize_register to add panels, sections, settings, and controls to the live theme customizer.
add_action( ‘customize_register’, function( WP_Customize_Manager $wp_customize ) { $wp_customize->add_setting( ‘hero_text’, [ ‘default’ => ‘Hello World’, ‘transport’ => ‘postMessage’, ]); $wp_customize->add_control( ‘hero_text’, [ ‘label’ => ‘Hero Text’, ‘section’ => ‘title_tagline’, ‘type’ => ‘text’, ]); } );
add_panel() add_section() add_setting() add_control() get_setting()
utility
wpdb
The global database class, accessed via $wpdb. Use for direct SQL when WP functions aren’t sufficient. Always use prepare() to prevent SQL injection.
global $wpdb; // Safe parameterized query $results = $wpdb->get_results( $wpdb->prepare( “SELECT * FROM {$wpdb->posts} WHERE post_author = %d AND post_status = %s”, $author_id, ‘publish’ ) ); $wpdb->insert( $wpdb->prefix . ‘my_table’, [ ‘col’ => ‘value’ ] );
get_results() get_row() get_var() insert() update() delete() prepare()

⚠ Pro Tips

  • Always call wp_reset_postdata() after a WP_Query loop — failing to do so corrupts the global $post for everything that follows.
  • Never access wpdb directly for post/user data — use the purpose-built query classes first for caching and hook benefits.
  • WP_Error is your friend: always wrap function returns in is_wp_error() before using the result.
  • When extending WP_List_Table, remember it’s a private API — it can change between WP versions. Add a version compatibility check.
  • Use WP_REST_Request::get_json_params() for JSON body data in REST callbacks, not get_param().
Helpful Resources & Links