我开始构建一个插件,其中需要使用wpdb
对象这是我第一次使用它。所以我查看了如何使用它。我看了一些例子,有一件事让我很困惑:upgrade.php
实际上,当我创建一个自定义查询时,只有当我包含upgrade.php
这样做:
require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
$custom_posts = $wpdb->get_results( \'SELECT * FROM \' . self::TABLE . \'\', OBJECT_K );
$wpdb->show_errors();
echo $wpdb->last_query;
return $custom_posts;
这只是我可以提供的方法示例之一。
有什么用upgrade.php
? 我的意思是,如果我不把它放进去,查询就不起作用了。请有人解释一下,因为法典中没有这方面的内容。
我真的有义务使用它吗?为什么?怎样
谢谢
更新:这里是完整的代码。
/**
* On activate or uninstall plugin
*/
register_activation_hook( __FILE__, array( \'Cpt\', \'init\' ) );
register_deactivation_hook( __FILE__, array( \'Cpt\', \'uninit\' ) );
register_uninstall_hook( __FILE__, array( \'Cpt\', \'uninit\' ) );
global $wpdb;
class Cpt
{
CONST TABLE = \'cpt\';
/**
* Constructor
*/
public function init()
{
/**
* Creation table for cpt
*/
$sql = "CREATE TABLE IF NOT EXISTS `" . self::TABLE . "` (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT \'0000-00-00 00:00:00\' NOT NULL,
name tinytext NOT NULL,
UNIQUE KEY id (id)
);";
require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
$wpdb->query($sql);
self::convert_to(\'post\', \'custom\');
}
public function uninit() {
/**
* Delete table for cpt
*/
require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
//$wpdb->query("DROP TABLE IF EXISTS `cpt`;");
//$this->delete_cpt("Custom post 1");
}
/**
* Add a custom post type
*/
public function add_cpt($name) {
/**
* Add a single data
*/
$time = (string)date(\'dmY\');
require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
$wpdb->insert(
self::TABLE,
array(
\'time\'=>$time,
\'name\'=>$name
)
);
/*$wpdb->show_errors();
echo $wpdb->last_query;
die();*/
}
/**
* Delete a custom post type
*/
public function delete_cpt($name) {
/**
* delete a single data
*/
require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
$wpdb->delete(
self::TABLE,
array(
\'name\'=>$name
)
);
$wpdb->show_errors();
echo $wpdb->last_query;
die();
}
/**
* Get all custom post types
*/
public static function get_ctps() {
/**
* delete a single data
*/
require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
$custom_posts = $wpdb->get_results( \'SELECT * FROM \' . self::TABLE . \'\', OBJECT_K );
$wpdb->show_errors();
echo $wpdb->last_query;
return $custom_posts;
}
/**
* Get a single custom post type
*/
public function get_ctp($name) {
/**
* get a single data
*/
require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
$custom_post = $wpdb->get_results( \'SELECT name FROM \' . self::TABLE . \' WHERE name = $name\', OBJECT_K );
$wpdb->show_errors();
echo $wpdb->last_query;
print_r($custom_post);
//return $custom_post;
}
/**
* Post type conversion
*/
public static function convert_to($from, $to) {
// Get the posts of the type $from
require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
$wpdb->update( \'wp_posts\', array(\'post_type\'=>$to), array(\'post_type\'=>$from) );
$wpdb->show_errors();
echo $wpdb->last_query;
}
/**
* Generate admin menu with all custom post types
*/
public function generate_menu() {
$custom_posts = self::get_ctps();
}
}