如果不再安装WordPress,如何从WordPress数据库中检索带有特色图像的帖子? [英] How can I retrieve posts with featured images from a WordPress database if WordPress is no longer installed?

查看:97
本文介绍了如果不再安装WordPress,如何从WordPress数据库中检索带有特色图像的帖子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a WordPress database in MySQL, but no longer have WordPress installed. How can I retrieve the posts and their featured images using only PHP and 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; ?>

这篇关于如果不再安装WordPress,如何从WordPress数据库中检索带有特色图像的帖子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆