WordPress に投稿されている記事を抽出できる関数に get_posts 関数があります。カテゴリや並び順など記事の抽出条件を指定して抽出できため非常に便利です。どのようなかたちで返されるのかすぐに分かるとスムーズにコードが書けるため、返り値のサンプルを記載します。
記事の数だけ配列になっており、その配列の各要素に記事のオブジェクトが格納されています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
array(5) { [0]=> object(stdClass)#58 (24) { ["ID"]=> int(50) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2012-01-03 01:52:30" ["post_date_gmt"]=> string(19) "2012-01-02 16:52:30" ["post_content"]=> string(18) "記事の本文" ["post_title"]=> string(4) "記事のタイトル" ["post_excerpt"]=> string(0) "記事の抜粋" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "記事のパスワード" ["post_name"]=> string(4) "記事のスラッグ" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2012-01-03 01:52:30" ["post_modified_gmt"]=> string(19) "2012-01-02 16:52:30" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(29) "http://example.com/?p=50" ["menu_order"]=> int(0) ["post_type"]=> string(4) "post" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } } |