如何使用源和图像获取wp_posts详细信息

时间:2014-11-18 05:55:27

标签: php mysql wordpress

我使用以下查询来获取tittle,data,tagss内容,slug,父ID。 这工作正常,但我需要获得与wp_postmeta表有关系的图像和图像源,所以我在第一个查询的while循环中使用了第二个查询

首先查询: -     选择wp_posts.ID为Id,wp_posts.post_title为标题,wp_posts.post_date为DATE,

  GROUP_CONCAT(wp_terms.name) AS TAGS, wp_posts.post_content as CONTENT,wp_terms.term_id,wp_terms.slug,wp_posts.post_parent as parent_id FROM wp_terms

  INNER JOIN wp_term_taxonomy ON wp_terms.term_id  = wp_term_taxonomy.term_id

  INNER JOIN wp_term_relationships ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id

  INNER JOIN wp_posts ON wp_posts.ID = wp_term_relationships.object_id

  WHERE post_type LIKE 'post' AND post_status LIKE 'publish' AND wp_terms.slug in ('interview','variety','lastpage','sport','arab-global','opinion','business','uae','firstpage')  

GROUP BY wp_posts.ID order by post_date limit 150

和sencond查询为

select select wp_postmeta.meta_value from wp_postmeta left join wp_posts on wp_postmeta.post_id in ('".$row['Id']."') = wp_posts.id in ('".$row['Id']."') and meta_key='_wp_attached_file'" 

但是我没有得到任何细节,所以我将关系与wp_post表的parent_id作为第三个查询而不是第二个查询。

select select wp_postmeta.meta_value from wp_postmeta left join wp_posts on wp_postmeta.post_id in ('".$row['parent_id']."') = wp_posts.id in ('".$row['parent_id']."') and meta_key='_wp_attached_file'"

之后,我使用单个查询来获取所有记录,如下所示

SELECT wp_posts.ID as Id,wp_posts.post_title as Title,wp_posts.post_date as DATE,wp_postmeta.meta_key,wp_postmeta.meta_value,
GROUP_CONCAT(wp_terms.name) AS TAGS, wp_posts.post_content as CONTENT,wp_terms.term_id,wp_terms.slug,wp_posts.post_parent as parent_id FROM wp_terms
INNER JOIN wp_term_taxonomy ON wp_terms.term_id  = wp_term_taxonomy.term_id
INNER JOIN wp_term_relationships ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id
INNER JOIN wp_posts ON wp_posts.ID = wp_term_relationships.object_id
INNER JOIN wp_postmeta ON(wp_posts.ID = wp_postmeta.post_id)
WHERE post_type LIKE 'post' AND post_status LIKE 'publish'
GROUP BY wp_posts.ID order by post_date limit 150

但是在那个查询中我有多个meta_key和多个元值,有任何有用的解决方案,所以我可以通过简单的sql和php获取wp_post详细信息及其源和图像

2 个答案:

答案 0 :(得分:1)

试试这个

$ meta_value_id ="从wp_postmeta中选择meta_value,其中meta_key =' _thumbnail_id'和post_id ='"。$ row [' Id']。"'&#34 ;;
                $ query_run_meta = mysql_query($ meta_value_id);                 $ row1 = mysql_fetch_assoc($ query_run_meta);

            $meta_image=  "select meta_value from wp_postmeta where meta_key='_wp_attached_file' and post_id='".$row1['meta_value']."'";   
            $query_meta=  mysql_query($meta_image);
            $row2=  mysql_fetch_assoc($query_meta);

现在您可以从$ row2获取图像源。

答案 1 :(得分:0)

从上面的查询中您获得了帖子ID,您可以使用以下函数来获取帖子图片网址:

$post_thumbnail_id = get_post_thumbnail_id( $post_id ); //It will return id of attached thumbnail
 $post_image_src= wp_get_attachment_image_src(post_thumbnail_id);// This will return source url of thumbnail