WordPress创建复合查询或使用一种帖子类型的数据访问另一种帖子类型

时间:2020-06-08 作者:PeterB

我已经创建了两种帖子类型,事件和注册,并希望能够循环浏览事件,然后获取每个事件的注册,但WP\\u查询似乎不起作用。

为9个事件和3个注册设置数据,其中两个用于一个事件,另一个用于另一个事件

它似乎无法正确获取注册数据。


            $events = new WP_Query(Array(
                \'posts_per_page\' => -1,
                \'post_type\' => \'event\'
            ));
            if ($events->have_Posts()){
                while($events->have_posts()){
                    $events->the_post();
                    echo "<p>Event " . get_the_title() . \'</p>\';
                    $eventID = $events->get_the_ID();
                    echo $eventID;
                    $args = array(
                        \'post_type\' => \'registration\',
                        \'meta_key\' => \'event_id\',
                        \'meta_value\' => $eventID,
                        \'meta_compare\' => \'=\'
                    );

                    $registrations = new WP_Query($args);
                    while ($registrations->have_posts()){
                        $registrations->the_post();
                        echo "<p>The registrations list "  . get_the_title() . "</p>";
                    }
                }
            }
我得到以下结果

Event 1
Event 2
Event 3
Event 4
Event 5
Event 6
Event 7
Event 8
Event 9
如果我换线\'meta_compare\' => \'=\'\'meta_compare\' => \'!=\'我得到以下信息

Event 1
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Event 2
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Event 3
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Event 4
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Event 5
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Event 6r Registration
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Event 7
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Event 8
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Event 9
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1

不知道为什么这会如此困难,可能是因为他们都是帖子,只是类型不同,来自同一张表。

有人能帮忙吗?

2 个回复
最合适的回答,由SO网友:ViralP 整理而成

根据WP documentation,

Using the WP_Meta_Query::parse_query_vars( $query ) method: 如果要使用简单查询参数,可以使用此方法(meta_key, meta_value, meta_type, meta_compare), 或者,如果您不确定是否存在元查询参数。

因此,我尝试转换meta_key, meta_valuemeta_compare 在源代码中进行封装meta_querykey, value, compare.

此外,添加了if( $registrations->have_post() ) {

您能检查一下下面的代码片段是否适合您吗?

            $events = new WP_Query(Array(
                \'posts_per_page\' => -1,
                \'post_type\' => \'event\'
            ));
            if ($events->have_posts()){
                while($events->have_posts()){
                    $events->the_post();
                    echo "<p>Event " . get_the_title() . \'</p>\';
                    $eventID = $events->get_the_ID();
                    echo $eventID;
                    $args = array(
                        \'post_type\' => \'registration\',
                        \'meta_query\' => array (
                          \'key\' => \'event_id\',
                          \'value\' => $eventID,
                          \'compare\' => \'=\'
                        )
                    );

                    $registrations = new WP_Query($args);
                    if( $registrations->have_post() ) { 
                      while ($registrations->have_posts()){
                        $registrations->the_post();
                        echo "<p>The registrations list "  . get_the_title() . "</p>";
                      }
                    }
                }
            }
编辑1:

如果event_id 是ACF,则应尝试使用ACF函数获取注册。

参考号:https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

echo $eventID;
$registrations = get_posts(array(
   \'numberposts\'   => -1,
   \'post_type\'     => \'registration\',
   \'meta_query\'    => array(
       array(
           \'key\'       => \'event_id\',
           \'value\'     => $eventID,
           \'compare\'   => \'=\',
       ),
   ),
));

if( $registrations->have_post() ) { 
 while ($registrations->have_posts()){
   $registrations->the_post();
   echo "<p>The registrations list "  . get_the_title() . "</p>";
 }
}

SO网友:PeterB

在你的帮助下,我成功地解决了这个问题,谢谢。我仍然使用WP\\u Query,但问题在于event\\u id是一个关系,因此是一个数组,因此“=”因为meta\\u类型不起作用,所以这里是工作代码。


            $events = new WP_Query(Array(
                \'posts_per_page\' => -1,
                \'post_type\' => \'event\',
                \'meta_key\' => \'event_date\',
                \'orderby\' => \'meta_value_num\',
                \'order\' => \'ASC\'
            ));
            if ($events->have_Posts()){
                while($events->have_posts()){
                    $events->the_post();
                    echo "<p>Event " . get_the_title() . \' \' . get_the_id() . \'</p>\';

                    $registrations = new WP_Query(Array(
                        \'posts_per_page\' => -1,
                        \'post_type\' => "registration",
                        \'meta_key\' => \'boat_type\',
                        \'orderby\' => \'meta_value\',
                        \'order\' => \'ASC\',
                        \'meta_query\' => array(
                          array(
                              \'key\'=> \'event\',
                              \'compare\' => \'LIKE\',
                              \'value\' => \'"\' . get_the_ID() . \'"\'
                          )
                        )
                      ));
                      if ($registrations->have_posts()){
                          while ($registrations->have_posts()){
                              $registrations->the_post();
                            echo "<p>---------reg title is: " . get_the_title() . " and id is " . get_the_ID() . "</p>";
                          }
                      }
                }
            }
现在显示

Event 1
Event 2
---------reg title is: Manual Registration and id is 221
Event 3
Event 4
---------reg title is: Registration and id is 217
---------reg title is: Another Registration and id is 222
Event 5
Event 6
Event 7
Event 8
Event 9