简体   繁体   中英

CodeIgniter to Query WordPress blog

I am finally attempting my hand at some CodeIgniter, and I'm stuck currently.

News Model

public function get_news($slug = FALSE){
    if($slug === FALSE){
        // if there is no slug specified, pull all records from the 'wp_posts' table
        $this->db->select('post_title, post_content, post_name');
        $this->db->where('post_status', 'published');
        $this->db->where('post_type', 'post');
        $this->db->order_by("post_date", "desc");
        $this->db->order_by("post_title", "asc");
        $query = $this->db->get('wp_posts');

        var_dump($query);

        // return the resulting array
        return $query->result_array();
    }else{
        // if it does exist, pull only the record(s) specified where column 'slug' = the parameter
        $this->db->select('post_title, post_content, post_name');
        $query = $this->db->get_where('wp_posts', array('post_name' => $slug));
        // return the resulting array
        return $query->row_array();
    }
}

Controller

class News extends CI_Controller{

    public function __construct(){
        parent::__construct();
        // located @ /application/models/news_model.php
        $this->load->model('news_model');   
    }

    // pull in all news items
    public function index(){
        $data['news'] = $this->news_model->get_news();
        // give the page a title
        $data['title'] = 'Articles Archive';
        $data['c_year'] = date('Y');
        // Load the header template
        $this->load->view('templates/header', $data);
        // Load the page
        $this->load->view('news/index', $data);
        // Load the footer template
        $this->load->view('templates/footer', $data);
    }

    // pull in only the news item(s) where the slug matches
    public function view($slug){
        $data['news_item'] = $this->news_model->get_news($slug);
        // give the page a title
        $data['title'] = $data['news_item']['title'];
        $data['c_year'] = date('Y');
        // Load the header template
        $this->load->view('templates/header', $data);
        // Load the page
        $this->load->view('news/view', $data);
        // Load the footer template
        $this->load->view('templates/footer', $data);
    }

}

Connection to the database is fine, but I am returning just the show_404(); in my view because there are no records.

Running a similar query direct against the database does indeed return all my records.

Here is the results of the var_dump :

object(CI_DB_mysql_result)#18 (8) { 
   ["conn_id"]=> resource(3) of type (mysql link persistent) 
   ["result_id"]=> resource(4) of type (mysql result) 
   ["result_array"]=> array(0) { } 
   ["result_object"]=> array(0) { } 
   ["custom_result_object"]=> array(0) { } 
   ["current_row"]=> int(0) ["num_rows"]=> int(0)
   ["row_data"]=> NULL 
}

What am I doing wrong here?

Change: $this->db->where('post_status', 'published');

To: $this->db->where('post_status', 'publish');

And the issue is cured... been a long week...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM