2012-04-18 9 views
1

私は外部のPHPファイルからWordPressデータベースを手動で照会しようとしています。タイトル、コンテンツ、最後の(または最初の)イメージ、そしてそのイメージのwp_postmetaから最後の3つの投稿を抽出します。サムネイルURL。投稿のタイトル、コンテンツ、画像の添付ファイルサムネイルを手動でクエリする方法はありますか?

タイトル、コンテンツ、イメージIDを取得できましたが、イメージサムネイルを取得するための別の結合を追加する方法を理解できませんでした。 58435, 6711, _wp_attachment_metadata, a:6:{s:5:"width";s:4:"1024";s:6:"height";s:3:"683"...

私はc.idに画像IDを取得し、それを必要とするすべてはまた、取得するための別のJOINで見ることができます:

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)表である:これは私が持っているものですフィールドのデータは meta_key="_wp_attachment_metadata" and post_id=c.idです。

誰でも質問を完了できますか?ありがとう!

答えて

-1

を作ります私は水平機能を追加しているので、別の解決策を選択しました(少なくとも一時的なもの):完全に空のカスタムページを作成しました(手動でいくつかのアクションを削除する必要がありましたs/filtersを空にする)、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

+0

次に、単にrssフィードを使用しないのはなぜですか? rssがあなたのニーズに合わない場合は、 "ソース"サイトでスクリプトを作成し(私の答えに記述されているようにWordPressの機能を使用)、それを "目的地"のサイトに含める必要があります。 – soju

+0

私は同じサーバー、同じホスティングアカウント、別のデータベースにいる場合、なぜRSSを使用する必要がありますか? :)これは(rssの機能はfeedburnerに移され、別のサーバーに移されています)もっと速くて簡単です。私は一歩足を踏み外しています。フルサイズの画像を取得できますが、別のJOINも必要ですその画像のサムネイルを取得します。最後のケースでは、サムネイルを取得するために別のクエリを使用しますが、私はそれが好きではありません:D – conualfy

関連する問題