如果不再安装WordPress,我如何从WordPress数据库中检索具有特色图像的帖子


How can I retrieve posts with featured images from a WordPress database if WordPress is no longer installed?

我在MySQL中有一个WordPress数据库,但不再安装WordPress。如何仅使用PHP和MySQL检索帖子及其特色图片?

您最好的选择是MySQLI。

<?php
$wp_database = new mysqli("yourhost", "yourusername", "yourpasssword", "yourdatabase");
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s'n", mysqli_connect_error());
    exit();
}
// Here's how you get all the posts. Adjust table names to suit.
$sql = "SELECT * FROM `wp_posts` WHERE `post_status` LIKE 'publish' AND `post_type` LIKE 'post'";
$result =  mysqli_query($wp_database, $sql);
$posts = mysqli_fetch_all($result, MYSQLI_ASSOC);
mysqli_free_result($result);
foreach ($posts as $post): ?>
<div>
    <?php print_r($post); ?>
</div>
<?php endforeach;
// And here's how you get an individual image url.
$post_id = 1181; // Your post ID here
$sql = "SELECT `guid` FROM `wp_posts` WHERE `id` IN (SELECT  `meta_value` 
FROM  `wp_postmeta` 
WHERE  `meta_key` LIKE  '_thumbnail_id'
AND  `post_id` = $post_id)";
$result = mysqli_query($wp_database, $sql);
$images = mysqli_fetch_all($result, MYSQLI_ASSOC);
mysqli_free_result($result);
foreach ($images as $image): ?>
<div>
    <?php print_r($image); ?>
</div>
<?php endforeach; ?>