If you need to perform a database query in WordPress, you can use the `select()` method of the `$wpdb` object. Here is an example:
<?php
// 引入WordPress的核心文件
require_once( dirname( __FILE__ ) . '/wp-load.php' );
// 执行查询操作
global $wpdb;
$table_name = $wpdb->prefix . 'my_table'; // 获取表名
$results = $wpdb->get_results( "SELECT * FROM $table_name WHERE name = 'John'" );
// 输出查询结果
if ( $results ) {
foreach ( $results as $row ) {
echo $row->name . ': ' . $row->email . '<br>';
}
} else {
echo 'No results found';
}In this example, we use the `get_results()` method from $wpdb to execute a query that retrieves all rows where the `name` field equals "John". The query statement is passed directly to the `get_results()` method as a string.
If the query is successful, $results will be an array containing each row of the query results. We can use a foreach loop to iterate over this array and output the values of the name and email fields for each row.
Please note that you need to replace "my_table" in the above example with the actual name of your table and modify the query statement as needed. Additionally, each row in the $results array is a standard object containing the value of each field from the query result; you can access these values using the object's properties.