web-dev-qa-db-ja.com

WP_Postオブジェクトを介したループ

このようなカスタムのWordPress投稿配列があります。

Array ( 
[1] => Array ( 
    [sno] => 1 
    [post] => WP_Post Object ( 
        [ID] => 1452 
        [post_author] => 12 
        [post_date] => 2017-06-04 18:09:47 
        [post_date_gmt] => 2017-06-05 01:09:47 
        [post_content] => Content here 
        [post_title] => Title here 
        [post_excerpt] => 
        [post_status] => publish 
        [comment_status] => open 
        [ping_status] => closed 
        [post_password] => 
        [post_name] => title-here 
        [to_ping] => 
        [pinged] => 
        [post_modified] => 2017-07-14 09:35:35 
        [post_modified_gmt] => 2017-07-14 16:35:35 
        [post_content_filtered] => 
        [post_parent] => 0 
        [guid] => https://urlhere.com&p=1452 
        [menu_order] => 0 
        [post_type] => sfwd-lessons 
        [post_mime_type] => 
        [comment_count] => 0 
        [filter] => raw 
    ) 
    [permalink] => https://urlhere.com/ 
    [sub_title] => 
    [status] => notcompleted 
    [sample] => is_not_sample 
    [lesson_access_from] => 
    )
)   

このロジックでこの配列の投稿をループしてみます。 「status」が「completed」の場合、出力タイトルと投稿のリンク。基本的なforeachループはうまくいくはずだと思いますが、うまくいかないようです。配列は次のように作成されます。

$lessons = learndash_get_course_lessons_list( $course_id );

私のforeachループはこんな感じです:

foreach ($lessons as $key => $lesson) {
  echo $lesson["post_title"];
}

これは何も返しません。配列データをループするために何をする必要がありますか?

1
Andrew

投稿が完了したことについてのあなたの言ったことに基づいて、あなたはそのステータスがcompletedであるかどうかをチェックし、そうであれば、タイトルを指すことができます。

foreach ( $lessons as $lesson ){
    if( $lesson['status'] == 'completed' ){
        echo $lesson['post']->post_title;
    }
}
2
Jack Johansson