ここはちょっと見せられない

ぜったいぜったい見せられない

記事中に画像のある記事だけを取得したい

意外に面倒。
これやろうと思ったら、WP本来のページングは使えない。

wp_posts には、画像は別の1レコードが記録されるようになっていて、そのpost_status はinheritとなる。post_parent に格納されるIDの記事が親記事となり別レコード。

親レコードの post_status を見ようにも get_posts では取れない。
あちこち見てると、query_posts だかを一回通しで見て、もう一回取得なんてものも見たけど、投稿が増えたらどうするんだ。全部総なめしちゃうのか。そりゃあ重たそうだ。

post_content の中に、src=〜 が含まれてるものでも取ってみようか。
いやでもそんな何だか変な匂いさせたくないし。


しょうがないので自前でSQL書く。
めんどくさ。

追記。

select 
  distinct ID, post_parent 
from wp_posts 
join wp_postmeta on wp_posts.ID = wp_postmeta.post_id 
where post_type = 'attachment' 
and meta_key = '_wp_attached_file' 
and post_parent in ( 
    select object_id 
    from wp_term_relationships 
    join wp_term_taxonomy using (term_taxonomy_id) 
    where term_id = <categoryid> 
) 
and post_parent in ( 
    select ID from wp_posts 
    where post_status = 'publish' 
) 
order by post_date desc

変態や変態。

natural join させてくれ。
全部usingさせてくれーっ。

>> and meta_key = '_wp_attached_file'
これ要らないな。distinct で十分だ。
取得してるのはattachmentで、postじゃないことに注意。