Created
February 5, 2017 19:42
-
-
Save qopqopqop/819d4dcbb982a5607a1d2555a881bae1 to your computer and use it in GitHub Desktop.
Get WordPress posts with CodeIgniter. A CodeIgniter Model Class to retrieve posts and images from your WordPress database.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @since July - 2013 | |
* @author: Fernando Porazzi - twitter @fernandoporazzi | |
* @see http://ellislab.com/codeigniter/user-guide/database/active_record.html | |
*/ | |
/** | |
* Set the number of posts you want to retrieve from you WP database | |
* Set the amount of images for every single post | |
*/ | |
class Blog extends CI_Model { | |
public function __construct(){ | |
parent::__construct(); | |
} | |
private $postLimit = 2; | |
private $imagesPerPost = 1; | |
public function getPosts () { | |
$this->db->where('wp_posts.post_parent', 0); | |
$this->db->where('wp_posts.post_status', 'publish'); | |
$this->db->limit($this->postLimit); | |
$this->db->order_by('post_date', 'DESC'); | |
$query = $this->db->get('wp_posts'); | |
$data = $query->result(); | |
$post = array(); | |
for ($i = 0; $i < count($data); $i++) { | |
array_push($post, | |
array( | |
'post' => $data[$i], | |
'image' => $this->getPostImages($data[$i]->ID) | |
) | |
); | |
} | |
//print_r($post); | |
return $post; | |
} | |
private function getPostImages ($idPost) { | |
$this->db->where('post_type', 'attachment'); | |
$this->db->where('post_parent', $idPost ); | |
$this->db->limit($this->imagesPerPost); | |
$this->db->order_by('post_date', 'DESC'); | |
$image = $this->db->get('wp_posts'); | |
if ($this->imagesPerPost > 1) { | |
return $image->result(); | |
} else { | |
return $image->row(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment