Last active
April 3, 2024 12:18
-
-
Save ericjuden/5136638 to your computer and use it in GitHub Desktop.
WordPress CRUD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Create a new record in a table | |
global $wpdb; | |
// data to be added | |
$data = array( | |
'post_title' => __('This is a test'), | |
'post_content' => __('This is really long text'), | |
'post_status' => __('draft') | |
); | |
// Data format | |
$data_format = array( | |
'%s', // post_title | |
'%s', // post_content | |
'%s' // post_status | |
); | |
$wpdb->insert('wp_posts', $data, $data_format); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Delete a record from a table | |
global $wpdb; | |
$where = array( | |
'id' => 5 | |
); | |
$where_format = array( | |
'%d' // id | |
); | |
$wpdb->delete('wp_posts', $where, $where_format) | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Run sql queries manually | |
global $wpdb; | |
// wpdb::prepare() requires 2 parameters. More info: http://make.wordpress.org/core/2012/12/12/php-warning-missing-argument-2-for-wpdb-prepare/ | |
$sql = $wpdb->prepare("SELECT * FROM " . $wpdb->posts . "WHERE post_title = %s", $_GET['title']); | |
$results = $wpdb->query($sql); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Reading data from the database | |
global $wpdb; | |
// wpdb::get_results() | |
$sql = "SELECT * FROM " . $wpdb->posts; | |
$results = $wpdb->get_results($sql); | |
// wpdb::get_row() | |
$sql2 = "SELECT * FROM " . $wpdb->posts ." WHERE post_title = 'This is a test'"; | |
$results2 = $wpdb->get_row($sql2); | |
// wpdb::get_col() | |
$sql3 = "SELECT post_status FROM " . $wpdb->posts; | |
$results3 = $wpdb->get_col($sql3); | |
// wpdb::get_var() | |
$sql4 = "SELECT post_status FROM " . $wpdb->posts . " WHERE post_title = 'This is a test'"; | |
$results4 = $wpdb->get_var($sql4); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Update a row in a table | |
global $wpdb; | |
// What values are we changing | |
$data = array( | |
'post_title' => __('This is another test'), | |
'post_content' => __('This is more text than previously entered'), | |
'post_status' => __('publish') | |
); | |
// What format is the data | |
$data_format = array( | |
'%s', // post_title | |
'%s', // post_content | |
'%s' // post_status | |
); | |
// What are we searching by | |
$where = array( | |
'post_title' => __('This is a test') | |
); | |
// What format is the search data in | |
$where_format = array( | |
'%s' | |
); | |
// Run the query | |
$wpdb->update('wp_posts', $data, $where, $data_format, $where_format); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment