结合 WP 课件功能、ACF 复读器字段和 meta_query 查找特定模板文件的复杂查询

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

我有一个非常复杂的查询,我无法按照我需要的方式开始工作。

我使用 WP 课件和 ACF 插件安装了 Wordpress。我需要显示与当前用户关联的课程页面。我希望链接引导用户进入课程 "home" 页面,用户在开始课程之前应该点击这些页面。我已经创建了课程 "home" 页面,但问题是 WP Courseware 无法将页面与课程相关联。所以我不得不使用 ACF 选项转发器将课程 ID 与任何必要的课程页面相关联。我知道这些关联页面之一是课程 "home" 页面的唯一方法是通过我用于课程主页的模板。

因此循环中的循环需要首先确定当前用户可以访问哪些课程,获取这些课程ID,循环ACF选项重复器以查找与这些课程ID关联的页面,然后在这些页面中循环查找哪一门(每门课程只有一门)使用课程主页模板。我发现的最后一个循环需要是一个 WP_Query 循环,因为这是查询 Wordpress 模板的唯一方法。

我陷入了循环,我正处于最艰难的时期。我认为使用 WP_Query 查询 Wordpress 模板和 ACF 转发器的数组 meta_queries 可能更简单和最直接(确定 ACF 转发器课程 ID 是否与用户的课程 ID 匹配可以访问)但我尝试查询 ACF 转发器子字段的尝试不起作用。

这是我的代码:

$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 as $user_course ) : 
    $course_id = $user_course->course_id;
    $course_title = $user_course->course_title;
    // Loop through the ACF course ID/page associations
    foreach ( $course_association_arr as $course_association ) :
        $assoc_course_id = $course_association['wp_courseware_id'];
        if ( $course_id == $assoc_course_id ) :
            // Loop through the ACF associated pages
            foreach ( $course_association['associated_pages'] as $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;

这会显示与用户课程关联的所有页面,而不仅仅是使用课程主页模板的页面。我必须以某种方式将 WP_Query 与这些 args 合并在一起,但我所做的一切都没有奏效:

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

如果我能以某种方式将 WP 查询转换为 if 语句(if template = page-course-home.php),我可以在关联页面查询中包含它以仅显示课程主页。或者也许还有另一种更出色的方法来做我需要做的事情。感谢所有反馈。

好的,我有事要做!我认为花这么多时间在这里提出问题帮助我找到了一种方法:

$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 as $user_course ) : 
    $course_id = $user_course->course_id;
    $course_title = $user_course->course_title;
    // Loop through the ACF course ID/page associations
    foreach ( $course_association_arr as $course_association ) :
        $assoc_course_id = $course_association['wp_courseware_id'];
        if ( $course_id == $assoc_course_id ) :
            // Loop through the ACF associated pages
            foreach ( $course_association['associated_pages'] as $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;

这看起来有点麻烦,但它确实有效。我不确定它是否会更好,但将 ACF 子字段查询合并到元查询中似乎更优雅,因此可以消除两个循环。如果有人对此有任何想法,我很乐意听取他们的意见。