[Custom Function] Get Posts by Author ID


Jun 08

If you are trying to get posts for a certain author/user , unfortunately there is no function offered by wordpress to do this so i wrote this function to help you do this

function get_posts_by_author($id,$num) {
	global $wpdb;
	$posts = $wpdb->get_results("SELECT * from $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' AND post_author = $id LIMIT 0,$num", ARRAY_A);
	return $posts;
}

this function takes two parameters
$id which is author id
$num which is number of posts you want to query

and it returns your posts in an array , so for example if you want to get the title of the first returned post you have to write this

$posts[0]['post_title']

Leave Your Own Comment on This Post

Spam protection by WP Captcha-Free




Copyright © 2012 Banhawi & web design egypt