wordpress queries tutorial

wordpress queries tutorial
Rate this post

wordpress-queries

How to include WordPress connection and functions in custom PHP

Always use include( 'wp-load.php' );

How to call WordPress database connection in PHP file

Always use global $wpdb;

How to run in WordPress DB in PHP file

Always use $wpdb->query( "SELECT * FROM wp_posts" );

How to run a query and get the results as an associative array

Always use $wpdb->get_results( "SELECT * FROM wp_posts" );

How to get a single variable in WordPress DB

Always use $wpdb->get_var( "SELECT post_title FROM wp_posts WHERE ID = 5" );

How to get a row as an Assoc. array

Always use $wpdb->get_row( "SELECT * FROM wp_posts WHERE ID = 1" );

How to get an entire column in WordPress DB

Always use $wpdb->get_col( "SELECT post_title FROM wp_posts" );

How to insert data into a table

Always use $wpdb->insert( 'wp_posts', array( 'post_title' => 'test', 'ID' => 8 ), array( '%s', '%d') );

How to update an existing row in WordPress DB

Always use $wpdb->update( 'wp_posts', array( 'post_title' => 'test2'), array( 'ID' => 5 ), array( '%s' ) );

How to SQL query in WordPress DB

Always use $wpdb->query( $wpdb->prepare( "UPDATE INTO wp_posts set post_title = %s WHERE ID = %d", 'test2', 2 ) );

How to SQL query in WordPress DB

Always use $wpdb->query( $wpdb->prepare( "UPDATE INTO wp_posts set post_title = %s WHERE ID = %d", 'test2', 2 ) );

no replies

Leave your comment