2012-04-18 59 views
1

我想從外部的php文件手動查詢WordPress的數據庫,我想提取最後3個職位:標題,內容,最後(或第一)圖像,並從該圖像,從wp_postmeta我想獲得縮略圖網址。如何手動查詢帖子標題,內容和圖片附件縮略圖?

我設法得到標題,內容,圖像ID,但我無法弄清楚如何添加另一個連接獲取圖像縮略圖。這是我有:

SELECT a.post_title title, max(c.guid) img_url, a.ID id 
FROM wp_posts a 

LEFT JOIN 
(select post_parent, max(post_date_gmt) as latest_image_date from wp_posts 
where post_type='attachment' GROUP BY post_parent) b 
on a.id=b.post_parent 

LEFT JOIN wp_posts c 
on c.post_parent=a.id 
and c.post_type='attachment' 
and b.latest_image_date = c.post_date_gmt where c.guid IS NOT NULL 

GROUP BY a.post_title ORDER BY a.ID 

的圖像縮略圖是wp_postmeta (meta_id, post_id, meta_key, meta_value)表,看起來像這樣:58435, 6711, _wp_attachment_metadata, a:6:{s:5:"width";s:4:"1024";s:6:"height";s:3:"683"...

我可以看到我得到的圖片ID在c.id和它所需要的是另一種JOIN也得到來自wp_postmeta的數據爲meta_key="_wp_attachment_metadata" and post_id=c.id

任何人都可以幫我完成查詢嗎?謝謝!

回答

-1

到底我選擇了另一個解決方案(至少是臨時的),因爲它增加了水平功能:我創建了一個完全空白的自定義頁面(我不得不手動刪除一些操作s /過濾器來獲得它爲空)在另一個站點(要包括的那個)以json格式導出數據。我得到這些數據並在當前網站中按照我的需要進行打印。

<?php 
$args= array(
    'posts_per_page' => 6 
    //if there are sticked posts they will also appear beside the 2 if we run this in a separate php file, not included in theme 
); 
$ii = 0; 
query_posts($args); 
if(have_posts()) : 


    while (have_posts()) : the_post(); 

    $permalink = get_permalink(); 
    $titlu = get_the_title(); 
    $excerpt = get_the_excerpt(); 

    $args = array(
     'post_type'  => 'attachment', 
     'post_parent' => $post->ID, 
     'post_status' => 'inherit', 
     'numberposts' => 1 
    ); 
    $images = get_posts($args); 
    $j = 1; 
     foreach ($images as $i) : 

     $ii++;//am preluat doar o poza, e ok numaratoarea 
     $j++; 
     $poza_thumb = wp_get_attachment_image_src($i->ID, 'thumbnail'); 
     $arr[$ii] = array('titlu' => $titlu, 'url' => $permalink, 'poza_thumb' => "".$poza_thumb[0], 'poza_width' => "".$poza_thumb[1], 'poza_height' => "". $poza_thumb[2], 'articol' => $excerpt); 

     endforeach; 

    endwhile; 

    echo json_encode($arr);//we send json as array 
else : 

endif;?> 
0

你應該節省您的時間和使用WordPress功能:

  • 做一個插件
  • 或包括WP-load.php在腳本

https://wordpress.stackexchange.com/questions/47049/what-is-the-correct-way-to-use-wordpress-functions-outside-wordpress-files

+0

感謝您的回覆。我想包括標題+鏈接+縮略圖的網站是wordpress的另一個安裝,所以不要將函數和全局變量混合在一起嗎? – conualfy 2012-04-18 13:34:15

+0

那麼爲什麼不簡單地使用RSS飼料?如果rss不符合您的需求,您應該在「源」網站上創建腳本(使用我的答案中描述的wordpress函數),並將其包含在「目標」網站上。 – soju 2012-04-18 14:22:29

+0

爲什麼我應該使用RSS如果我在同一臺服務器上,相同的託管帳戶,只是不同的數據庫? :)是這樣很快(rss功能被移動到feedburner,所以另一個服務器),它應該更容易,因爲我只是一步之遙:我可以得到全尺寸的圖像,但我只需要另一個JOIN獲取該圖像的縮略圖。在最後一種情況下,我將使用另一個查詢來獲取縮略圖,但我不喜歡:D – conualfy 2012-04-18 14:52:44

相關問題