WordPress

WordPress TutorialRemove Version from WordPress and StylesheetsRun WordPress local with XAMPPSecurity in WordPress - EscapingSecurity in WordPress - SanitizationThe Loop (main WordPress loop)Update WordPress ManuallyWordPress Actions and FiltersWordPress add action()WordPress add editor style()WordPress add menu page()WordPress Add ShortcodeWordPress add submenu page()WordPress add theme support()WordPress Add/remove contact info for users with user contactmethods filter hookWordPress Admin Dashboard WidgetsWordPress AJAXWordPress Alternating main loop (pre get posts filter)WordPress Child Theme BasicsWordPress Create a Post ProgrammaticallyWordPress Create Template for Custom Post TypeWordPress Creating a custom templateWordPress Custom exerpts with excerpt length and excerpt moreWordPress Custom Post TypesWordPress Customizer Basics (Add Panel, Section, Setting, Control)WordPress Customizer Hello WorldWordPress DebuggingWordPress Enqueuing scriptsWordPress Enqueuing StylesWordPress Function : wp trim words()WordPress Function: add action()WordPress get bloginfo()WordPress get home path()WordPress get option()WordPress get permalink()WordPress get template part()WordPress get the category()WordPress get the title()WordPress home url()WordPress How Can I integrate Markdown editor with Advance Custom Fields repeater Add-on.WordPress initWordPress Installation and ConfigurationWordPress Making network requests with HTTP APIWordPress Meta BoxWordPress Options APIWordPress Plugin creationWordPress Plugin developmentWordPress Post FormatsWordPress Querying postsWordPress Remove Auto Line Breaks from Content and ExcerptWordPress REST APIWordPress Secure your installationWordPress ShortcodeWordPress Shortcode with attributeWordPress ShortcodesWordPress SidebarsWordPress Site MigrationWordPress TaxonomiesWordPress Template hierarchyWordPress template includeWordPress the $wpdb ObjectWordPress the Admin Bar (aka The Toolbar)WordPress the title()WordPress theme and child-theme developmentWordPress ThemesWordPress wp get current user()WordPress WP Query() LoopWordPress WP-CLIWordPress WP-Cron



WordPress the $wpdb Object

From WikiOD

Remarks[edit | edit source]

There are two ways to reference the $wpdb object. The first is to use the PHP keyword global in order to act on the global instance of the object.

global $wpdb;
echo $wpdb->prefix;
// Outputs the prefix for the database

The second way to use the $wpdb object is to reference PHP's $GLOBALS super global variable.

echo $GLOBALS['wpdb']->prefix;
// This will also output the prefix for the database

The second way is discouraged as it may not be considered the best practice.

Selecting a variable[edit | edit source]

In the most basic form, it is possible to select a single variable from a table by calling the object's get_var method passing in an SQL query.

global $wpdb;
$user = $wpdb->get_var( "SELECT ID FROM $wpdb->users WHERE user_email='foo@bar.com'" );

It is very important to note that any untrusted values used in queries must be escaped in order to protect against attacks. This can be done using the object's prepare method.

global $wpdb;
$email = $_POST['email'];
$user = $wpdb->get_var(
    $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE user_email=%s", $email )
);
if( !is_null( $user ){
    echo $user; 
} else {
    echo 'User not found';
}

Selecting multiple rows[edit | edit source]

You can use get_results to get multiple rows from database.

global $wpdb;
$userTable =$wpdb->prefix."users";

$selectUser = $wpdb->get_results("SELECT * FROM $userTable");

This will show all users list in array.

Credit:Stack_Overflow_Documentation