php - A complicated query combining a WP Courseware function, ACF repeater fields and a meta_query looking for a particular template file -


i have pretty complicated query have not been able work way need to.

i have wordpress install using plugins wp courseware , acf. need display page of courses associated current user. want links lead user course "home" pages user should hit prior starting course. have created course "home" pages, problem wp courseware has no way associate page course. had use acf options repeater associates course id whatever course pages necessary. way know 1 of associated pages course "home" page template use course home pages.

so loops within loops need first determine courses current user has access to, course ids, loop acf options repeater find pages associated course ids, of pages loop find out 1 (there 1 per course) uses course home page template. last loop discovered needs wp_query loop that's way query wordpress template.

i lost in loops , i'm having hardest time. thought might simpler , direct use wp_query query array meta_queries of both wordpress template , acf repeater (to determine acf repeater course id matches course id user has access to) attempts @ querying acf repeater sub fields not working.

here's code:

$user = wp_get_current_user(); $user_id = $user->id; $user_course_list = wpcw_users_getusercourselist($user_id); $course_association_arr = get_field('course_association', 'option'); // loop through user's courses foreach ( $user_course_list $user_course ) :      $course_id = $user_course->course_id;     $course_title = $user_course->course_title;     // loop through acf course id/page associations     foreach ( $course_association_arr $course_association ) :         $assoc_course_id = $course_association['wp_courseware_id'];         if ( $course_id == $assoc_course_id ) :             // loop through acf associated pages             foreach ( $course_association['associated_pages'] $associated_page ) :                 $page_id = $associated_page->id;                 $page_url = $associated_page->guid;                 echo '<li><a href="' . $page_url . '">'. $course_title . '</a></li>';             endforeach;         endif;     endforeach; endforeach; 

this displays pages associated user's courses, not ones using course home template. somehow have incorporate wp_query these args in there , nothing have done has worked:

$args = array(     'post_type' => 'page',     'meta_query' => array(         array(             'key' => '_wp_page_template',             'value' => 'page-course-home.php',         ),     ) ); 

if somehow turn wp query if statement (if template = page-course-home.php) have inside associated pages query show course home pages. or there maybe more brilliant way need do. appreciate feedback.

ok got work! think spending time framing question here helped me see 1 way it:

$user = wp_get_current_user(); $user_id = $user->id; $user_course_list = wpcw_users_getusercourselist($user_id); $course_association_arr = get_field('course_association', 'option'); // loop through user's courses foreach ( $user_course_list $user_course ) :      $course_id = $user_course->course_id;     $course_title = $user_course->course_title;     // loop through acf course id/page associations     foreach ( $course_association_arr $course_association ) :         $assoc_course_id = $course_association['wp_courseware_id'];         if ( $course_id == $assoc_course_id ) :             // loop through acf associated pages             foreach ( $course_association['associated_pages'] $associated_page ) :                 $page_id = $associated_page->id;                 $page_url = $associated_page->guid;                 $args = array(                     'post_type' => 'page',                     'page_id' => $page_id,                     'meta_query' => array(                         array(                             'key' => '_wp_page_template',                             'value' => 'page-course-home.php',                         ),                     )                 );                 $course_assoc_pages = new wp_query( $args );                 if( $course_assoc_pages->have_posts() ) :                     while ( $course_assoc_pages->have_posts() ) : $course_assoc_pages->the_post();                         echo '<li><a href="' . $page_url . '">'. $course_title . '</a></li>';                     endwhile;                 endif;                 wp_reset_query();             endforeach;         endif;     endforeach; endforeach; 

this seems bit cumbersome, works. i'm not sure if better seems more elegant incorporate acf subfield query meta query, eliminate 2 of loops. if has thoughts on love hear them.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -