Pacman
Pacman

Reputation: 2245

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 havingis, 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

Upvotes: 5

Views: 13403

Answers (3)

inigomedina
inigomedina

Reputation: 1831

Usual way to read from database in WordPress is the following:

  1. get global variable $wpdb

    global $wpdb
  2. 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'";
  3. method get_results() retrieves values from db

    $posts = $wpdb->get_results($sql);
    $output .= '';
    foreach ($posts as $post) {
    $output .= '
  4. post_name). '">'.strip_tags($post->post_title).'
  5. '; } $output .= ''; echo $output;

Upvotes: 6

janw
janw

Reputation: 6662

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

Upvotes: 5

Leandro Bardelli
Leandro Bardelli

Reputation: 11598

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

Upvotes: 0

Related Questions