Get Articles from a specific cagetory in Wordpress

After googling and not found any code, I made my own script to get articles from wordpress with a specific category.

Category = term_taxonomy_id // you can see it in the database table called wp_term_taxonomy

Here is the code:

[sourcecode language='php']

//////////////////////////////////////////////////////////////////////

$conn = mysql_connect(‘localhost’,'root’,”);
mysql_select_db(“keithics”,$conn) or die(mysql_error());

if (!$conn) {
die(‘Error!!–> ‘ . mysql_error()); // if there’s an error, e.g. wrong password or username.
}

$sql = “Select wp_posts.* ,wp_term_relationships.* from `wp_posts`,`wp_term_relationships` where wp_posts.post_status = ‘publish’ and wp_term_relationships.term_taxonomy_id = 7 and wp_posts.ID = wp_term_relationships.object_id order by rand() limit 5 “;

$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo ‘‘.$row['post_title'].’‘;
}

[/sourcecode]

Leave a Reply