复杂的查询,结合了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

查看:98
本文介绍了复杂的查询,结合了WP课件功能,ACF转发器字段和寻找特定模板文件的meta_query的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a pretty complicated query that I have not been able to get to work the way I need it to.

我使用WP Courseware和ACF插件安装了Wordpress。我需要显示与当前用户关联的课程页面。我希望这些链接将用户引导至用户在开始课程之前应点击的课程主页页面。我已经创建了课程主页页面,但是问题是WP Courseware无法将页面与课程相关联。因此,我不得不使用ACF选项中继器,将课程ID与所需的任何课程页面相关联。我唯一知道这些关联页面之一是课程主页是由我用于课程主页的模板构成的。

I have a Wordpress install using the plugins WP Courseware and ACF. I need to display a page of courses associated with the current user. I want the links to lead the user to the course "home" pages that the user should hit prior to starting the course. I have created course "home" pages, but the problem is WP Courseware has no way to associate a page with a course. So I had to use an ACF options repeater that associates the course ID with whatever course pages are necessary. The only way I know that one of those associated pages is the course "home" page is by the template I use for course home pages.

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

So the loops within loops need to first determine what courses the current user has access to, get those course IDs, loop the ACF options repeater to find the pages associated with those course IDs, then of those pages loop to find out which one (there is only one per course) uses the course home page template. This last loop I discovered needs to be a WP_Query loop as that's the only way to query for a Wordpress template.

我在循环中迷失了,我遇到了最困难的事情时间。我认为使用WP_Query来查询Wordpress模板和ACF中继器的数组meta_queries可能更简单,最直接(确定ACF中继器的课程ID是否与用户可以访问的课程ID匹配),但是我尝试查询ACF转发器子字段不起作用。

I am lost in loops and I'm having the hardest time. I thought it might be simpler and most direct to use the WP_Query to query an array meta_queries of both the Wordpress template and the ACF repeater (to determine is the ACF repeater course ID matches the course ID the user has access to) but my attempts at querying ACF repeater sub fields is not working.

这是我的代码:

$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中,而我没有做过的工作:

This displays all pages associated with a user's courses, not just the ones using the course home template. I somehow have to incorporate a WP_Query with these args in there and nothing I have done has worked:

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

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

If I could somehow turn the WP query into an if statement (if template = page-course-home.php) I could have that inside the associated pages query to only show course home pages. Or there maybe another more brilliant way to do what I need to do. I appreciate all feedback.

推荐答案

好,我有一些工作要做!我认为花这么多时间在这里为这个问题定框可以帮助我看到我可以做到的一种方法:

Ok I got something to work! I think spending so much time framing the question here helped me see one way I could do 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 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子字段查询合并到元查询中似乎更好,因此可以消除两个循环。如果有人对此有任何想法,我很想听听他们的意见。

This seems a bit cumbersome, but it works. I'm not sure if it would be better but it seems more elegant to incorporate the ACF subfield query into the meta query, so could eliminate two of the loops. If anyone has any thoughts on this I would love to hear them.

这篇关于复杂的查询,结合了WP课件功能,ACF转发器字段和寻找特定模板文件的meta_query的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆