Load WordPress / WPML posts in PHP

When I was programming my portfolio I had to show the latest posts of my blog. This blog is coded in WordPress using the multi language plugin WPML. So, in order to obtain the latest posts I have consulted the tables where there are the information I need.

Assuming you have ‘wp_’ as the prefix for your WordPress tables, the default post table is wp_posts and the table used to matching each post and the language is wp_icl_translations. The matching is strengthened by the field id of wp_posts and the field element_id of wp_icl_translations.

The code is:

$server = "server's name";
$user = "database's username";
$password = "database's user password";
$database = 'database's name';
MYSQL_CONNECT($server, $user, $password) or die('MySQL connection error');
MYSQL_SELECT_DB($database) or die('The BD wasn't found');
$posts = MYSQL_QUERY("SELECT p.* FROM wp_posts p, wp_icl_translations t WHERE p.post_status='publish' AND p.post_type='post' AND p.ID=t.element_id AND t.element_type='post_post' AND t.language_code='" . $languageCode . "' ORDER BY p.post_date DESC") or die('Error in query');

Where languageCode is the language shortcode (‘en’ -> English, ‘es’ -> Spanish).

Finally, must go through $posts and format the info. For example, the post’s title is in the ‘post_title’ index of each element of $posts, the date is in ‘post_date’, etc.

This entry was tagged as

Leave a Reply

Your email address will not be published.