sql >> Databasteknik >  >> RDS >> Mysql

Hur kan jag hämta inlägg med utvalda bilder från en WordPress-databas om WordPress inte längre är installerat?

Det bästa skulle vara 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; ?>



  1. MySQL Hur man infogar i [temp tabell] FRÅN [Lagrad procedur]

  2. Töm IN-klausulparameterlistan i MySQL

  3. mysql CONCAT () AS namn?

  4. Inkrementerar ett fält i MySQL atomärt?