WordPress Posts (wp_posts)

wp_posts is a PHP class for pulling WordPress posts directly out of a WordPress database. wp_posts makes it possible to use WordPress as a simple and admin tool without having to use the entire front-end and theme code.

It's easy to integrate with either a normal WordPress installation or a multi-site installation.


Examples

This page has the following examples of the wp_posts class in action.

  1. Initializing the class
  2. Pulling all the posts
  3. Pulling a single post by ID and by name (slug)
  4. Pull all posts in a category
  5. Pull first 5 posts between two dates
  6. Pull posts matching certain custom fields (meta)

1. Initializing the class

[Back to the Top]

<?php
$connection = mysql_connect($host,$user,$pass);
include('wp_posts.php');
$p = new wp_posts($connection);
?>

2. Pulling all the posts

[Back to the Top]

	foreach($p->get_posts() as $post) {
		display_post_short($post);
	}
	

Second Post

ID: 6

post name (slug): second-post

post date: 2010-11-11 17:04:00

post modified: 2010-11-11 17:05:38

post status: publish

post type: post


Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed nec ipsum lorem. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Etiam malesuada pulvinar libero rhoncus lacinia. Nam interdum tempor dui vitae dictum. Cras dictum justo in ipsum vehicula eget vehicula tortor pharetra. Sed laoreet pellentesque ante quis lobortis. Nullam diam dolor, rhoncus ut hendrerit eu, viverra ac eros. Nulla non turpis dolor. In varius tempor leo, eget tempor velit auctor quis. Nam hendrerit, metus in euismod gravida, neque neque iaculis purus, a euismod libero nulla non turpis. Vestibulum eros libero, ultrices non fermentum id, adipiscing nec eros. Aenean ante tortor, tristique a laoreet vitae, ultrices ac sem. Phasellus tortor sem, porttitor sed placerat sit amet, mollis eu tellus. Morbi eleifend accumsan libero, ut consequat nunc malesuada a. Nunc eget eros non nulla scelerisque blandit eu sed ligula. Nunc elementum sem ut lacus imperdiet feugiat. Etiam eget eros a quam convallis commodo.

Nulla ultricies, lacus sit amet eleifend sodales, lacus libero suscipit massa, non vestibulum quam odio ac orci. Sed dictum magna id velit ornare mollis. Suspendisse potenti. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Curabitur a urna non purus accumsan pharetra. Vestibulum tristique, turpis ut facilisis auctor, eros metus posuere lorem, vel pellentesque justo sem quis orci. Morbi eleifend turpis ac eros tristique pellentesque. Vestibulum lobortis risus ac ante ornare tempus. Etiam nec turpis nec dui faucibus tincidunt. Ut eu libero magna. Nullam tortor lorem, pharetra a vehicula id, tempus in quam. Ut sem nunc, fermentum ac varius sit amet, sodales ut tellus. Fusce vel risus elit, vel convallis enim. Curabitur eu quam nunc. In hac habitasse platea dictumst.

Hola World

ID: 22

post name (slug): hola-world

post date: 2010-11-11 15:39:05

post modified: 2010-11-11 15:39:05

post status: publish

post type: post


asdfasdfasdf asdfasdf

categorized as in ‘cat 1′

Gutentag, world

ID: 20

post name (slug): gutentag-world

post date: 2010-11-11 15:38:44

post modified: 2010-11-11 15:38:44

post status: publish

post type: post


asdfasdfasdfasd asdfasd fas dfasdfsdf

Bojour, World

ID: 18

post name (slug): bojour-world

post date: 2010-11-11 15:38:25

post modified: 2010-11-11 15:45:04

post status: publish

post type: post


asldkf asdlfkj aslkdfj asdlkfjsd

asldkf asdlfkj aslkdfj asdlkfjsd

asldkf asdlfkj aslkdfj asdlkfjsd

asldkf asdlfkj aslkdfj asdlkfjsd

ID: 12

post name (slug): 12

post date: 2010-11-11 15:25:04

post modified: 2010-11-11 15:31:55

post status: publish

post type: post


Here’s a new post where that is super popular.

A custom field has been set for it where name = ‘popularity’ and value = ‘5′

this post is also categorized in “cat 1″ which has the category slug = “cat-1″

asdf

ID: 9

post name (slug): asdf

post date: 2010-11-11 15:06:20

post modified: 2010-11-11 15:06:20

post status: publish

post type: post


asdf

Hello world!

ID: 1

post name (slug): hello-world

post date: 2010-11-10 22:34:15

post modified: 2010-11-11 22:48:38

post status: publish

post type: post


Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!

3. Pulling a single post by ID and by name (or slug)

[Back to the Top]

	display_post_short($p->get_post(1));
	

Hello world!

ID: 1

post name (slug): hello-world

post date: 2010-11-10 22:34:15

post modified: 2010-11-11 22:48:38

post status: publish

post type: post


Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!

	display_post_short($p->get_post('hello-world'));
	

Hello world!

ID: 1

post name (slug): hello-world

post date: 2010-11-10 22:34:15

post modified: 2010-11-11 22:48:38

post status: publish

post type: post


Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!

The function will return the following raw array.

Array
(
    [ID] => 1
    [post_author] => 1
    [post_date] => 2010-11-10 22:34:15
    [post_date_gmt] => 2010-11-10 22:34:15
    [post_content] => Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!
    [post_title] => Hello world!
    [post_excerpt] => 
    [post_status] => publish
    [comment_status] => open
    [ping_status] => open
    [post_password] => 
    [post_name] => hello-world
    [to_ping] => 
    [pinged] => 
    [post_modified] => 2010-11-11 22:48:38
    [post_modified_gmt] => 2010-11-11 22:48:38
    [post_content_filtered] => 
    [post_parent] => 0
    [guid] => http://sjparsons.com/wp-posts-demo/?p=1
    [menu_order] => 0
    [post_type] => post
    [post_mime_type] => 
    [comment_count] => 1
    [post_content_formatted] => 

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!

)

4. Pull all posts in a category.

[Back to the Top]

The following statement will pull all the posts in the 'cat 1' category which has slug of 'cat-1'. In the 'categories' array you can put a list of categories that the posts should be in.

	$options = array(
		'categories' => array('cat-1'),
		);
	foreach($p->get_posts($options) as $post) {
		display_post_short($post);
	}
	

Hola World

ID: 22

post name (slug): hola-world

post date: 2010-11-11 15:39:05

post modified: 2010-11-11 15:39:05

post status: publish

post type: post


asdfasdfasdf asdfasdf

categorized as in ‘cat 1′

ID: 12

post name (slug): 12

post date: 2010-11-11 15:25:04

post modified: 2010-11-11 15:31:55

post status: publish

post type: post


Here’s a new post where that is super popular.

A custom field has been set for it where name = ‘popularity’ and value = ‘5′

this post is also categorized in “cat 1″ which has the category slug = “cat-1″

5. Pull 3 earliest posts between two dates.

[Back to the Top]

The following statements will pull 3 posts in time ascending order between Sept 1 1978 and tomorrow. The strings in the time_span array can be any string that when strtotime() is applied yield a valid timestamp. The first string is the beginning of the time span, the second is the end.

	$options = array(
		'limit' => 3,
		'order' => 'time_asc',
		'time_span' => array('Sept 1, 1978','tomorrow'),
		);
	foreach($p->get_posts($options) as $post) {
		display_post_short($post);
	}
	

Hello world!

ID: 1

post name (slug): hello-world

post date: 2010-11-10 22:34:15

post modified: 2010-11-11 22:48:38

post status: publish

post type: post


Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!

asdf

ID: 9

post name (slug): asdf

post date: 2010-11-11 15:06:20

post modified: 2010-11-11 15:06:20

post status: publish

post type: post


asdf

ID: 12

post name (slug): 12

post date: 2010-11-11 15:25:04

post modified: 2010-11-11 15:31:55

post status: publish

post type: post


Here’s a new post where that is super popular.

A custom field has been set for it where name = ‘popularity’ and value = ‘5′

this post is also categorized in “cat 1″ which has the category slug = “cat-1″

6. Pull posts matching certain custom fields (meta).

[Back to the Top]

It's possible to use the custom fields or meta to determine whether a post is returned or not.

The following statement pulls all posts where a custom field (or meta) is set for key = 'feature_show' and value like '%1%'

	$options = array(
		'meta_filter' => array( 'key' => 'feature_show', 'value'=> 1);
		);
	foreach($p->get_posts( $options) as $post) {
		display_post_short($post);
	}
	

asdf

ID: 9

post name (slug): asdf

post date: 2010-11-11 15:06:20

post modified: 2010-11-11 15:06:20

post status: publish

post type: post


asdf

The following statement pulls all posts where a custom field (or meta) is set for key = 'popularity' and value > '3'

	$options = array(
		'meta_filter' => array( 'key' => 'popularity', 'value'=> 3, 'type' => '>'),
		);
	foreach($p->get_posts( $options) as $post) {
		display_post_short($post);
	}
	

ID: 12

post name (slug): 12

post date: 2010-11-11 15:25:04

post modified: 2010-11-11 15:31:55

post status: publish

post type: post


Here’s a new post where that is super popular.

A custom field has been set for it where name = ‘popularity’ and value = ‘5′

this post is also categorized in “cat 1″ which has the category slug = “cat-1″

The source code has clear documentation of all the possible options.