reading from a database using WordPress
I am very new to WordPress, I have very little knowledge with PHP. I read about PODS and I know how to create one and use pages / templates to display data.
The issue I am havingi开发者_运维问答s, the PODS I was creating use static data entered via the WP dashboard, what I want is to read data from a database, I am using MySql (same DB that wordpress is using). is there a way to use PODS and read the data from the DB, or wordpress has a better way to handle data coming from the DB ?
Thanks
You should Look into the $wpdb variable (and class)
http://codex.wordpress.org/Class_Reference/wpdb
Do remember to declare it a global:
<?php global $wpdb; ?>
I am however not sure what you want.
I advise staying close to wordpress.
If you want to create your own custom post types without using code use moretypes
Usual way to read from database in WordPress is the following:
get global variable $wpdb
global $wpdb
prepare the output and SQL command
$output = ""; $sql = "SELECT ".$wpdb->prefix."posts.post_title, ".$wpdb->prefix."posts.post_name FROM ". $wpdb->prefix."posts WHERE ".$wpdb->prefix. "posts.post_status='publish' AND ".$wpdb->prefix. "posts.post_parent=0 AND ".$wpdb->prefix. "posts.post_type='sometype'";
method get_results() retrieves values from db
$posts = $wpdb->get_results($sql); $output .= ''; foreach ($posts as $post) { $output .= '
- post_name). '">'.strip_tags($post->post_title).' '; } $output .= ''; echo $output;
Wordpress CSM has a very good class to work with db, i think the better bet on this is learn how db connects and get data from mysql
精彩评论