查询Wordpress缩略图

时间:2010-08-31 10:49:52

标签: sql wordpress thumbnails

我正在尝试获取wordpress博客中帖子的缩略图元数据及其类别和其他一些数据。 缩略图数据已序列化,稍后我将对其进行反序列化。

以下是我的查询:

    SELECT wpostmeta.meta_value AS url,
    wposts.post_content, wposts.post_title, wposts.post_status, wposts.post_name,
    cat_terms.name AS category_name, cat_terms.term_id AS category_id,
    ttable.meta_value AS thumb_data
    FROM `wp_postmeta` AS wpostmeta,
    `wp_posts` AS wposts
    INNER JOIN wp_term_relationships AS cat_term_relationships ON (wposts.ID = cat_term_relationships.object_id)
    INNER JOIN wp_term_taxonomy AS cat_term_taxonomy ON (cat_term_relationships.term_taxonomy_id = cat_term_taxonomy.term_taxonomy_id AND cat_term_taxonomy.taxonomy = 'category')
    INNER JOIN wp_terms AS cat_terms ON (cat_term_taxonomy.term_id = cat_terms.term_id)
    LEFT JOIN `wp_postmeta` AS ttable ON (wposts.ID = ttable.post_id AND ttable.meta_key = '_wp_attachment_metadata')
    WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = 'url'
    AND wposts.post_type = 'post'

除附件部分外,一切正常。 'thumb_data'为NULL。

你可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

此查询解决了我的问题:

$sql = "SELECT wposts.ID AS ID, thumbposts.ID AS thumbposts_ID,
        postmeta.meta_value AS url,
        wposts.post_content, wposts.post_title, wposts.post_status, wposts.post_name,
        cat_terms.name AS category_name, cat_terms.term_id AS category_id,
        thumb.meta_value AS thumb_data

        FROM `wp_postmeta` AS postmeta,
        `wp_posts` AS thumbposts,
        `wp_postmeta` AS thumb,
        `wp_posts` AS wposts

        INNER JOIN wp_term_relationships AS cat_term_relationships ON (wposts.ID = cat_term_relationships.object_id)
        INNER JOIN wp_term_taxonomy AS cat_term_taxonomy ON (cat_term_relationships.term_taxonomy_id = cat_term_taxonomy.term_taxonomy_id AND cat_term_taxonomy.taxonomy = 'category')
        INNER JOIN wp_terms AS cat_terms ON (cat_term_taxonomy.term_id = cat_terms.term_id)

        WHERE wposts.ID = postmeta.post_id
        AND postmeta.meta_key = 'url'

        AND thumbposts.post_parent = wposts.ID
        AND thumbposts.post_type = 'attachment'

        AND thumb.post_id = thumbposts.ID
        AND thumb.meta_key = '_wp_attachment_metadata'

        AND wposts.post_type = 'post'";

    if($filter == 1){
        $sql .= " AND cat_terms.term_id = '1'";
    }
    if($filter == 2){
        $sql .= " AND cat_terms.term_id = '3'";
    }

    $sql .= " GROUP BY wposts.ID
        ORDER BY wposts.post_date ASC";

答案 1 :(得分:0)

您能否准确解释您想要实现的目标 - 在很多情况下,没有必要直接进入数据库。

例如,您可以将get_posts与'post_type'=>'attachment'和'post_mime_type'=>'image'一起使用以获取图像附件,然后输出元数据,并且内置了许多功能来处理调整大小,显示和裁剪缩略图,例如get_post_thumbnail()等。