Reputation: 35
I am trying to get posts from an external website to show on my own website and I am simply achieving this with the wordpress $wpdb
class.
I have managed to display the titles of the posts from the external website on to my website however, I am having trouble displaying the featured image that is associated with it's own post.
Any help would be appreciated.
<?php
$mydb = new wpdb('root','','test','localhost');
$rows = $mydb->get_results(" select post_title, guid FROM wp_posts WHERE post_parent = '21' ");
echo '<ul>';
foreach ($rows as $obj) : ?>
<li><a href="http://localhost/database2/2015/11/26/<?php echo $obj->post_title; ?>"><?php echo $obj->post_title; ?></a></li>
<?php echo $obj->meta_value; ?>
<?php
endforeach;
echo '</ul>';
?>
Upvotes: 0
Views: 639
Reputation: 35
I managed to get the associated featured image for a post from the external database using the following SQL query below:
"SELECT
p1.*,
wm2.meta_value
FROM
wp_posts p1
LEFT JOIN
wp_postmeta wm1
ON (
wm1.post_id = p1.id
AND wm1.meta_value IS NOT NULL
AND wm1.meta_key = '_thumbnail_id'
)
LEFT JOIN
wp_postmeta wm2
ON (
wm1.meta_value = wm2.post_id
AND wm2.meta_key = '_wp_attached_file'
AND wm2.meta_value IS NOT NULL
)
WHERE
p1.post_status= %s
AND p1.post_type=%s
ORDER BY
p1.post_date DESC
LIMIT 4", 'publish','post'));
Upvotes: 0