2014-01-19 46 views
1

我会尝试并尽可能简单地解释这一点。基于关系型子元素的wordpress循环的php查询

我有3种类型的游戏在这里玩。

  1. 报告
  2. 事件(摩托车,2013年,摩托车-2014)
  3. 电路

在我的 '事件' single.php中显示事件信息,我想创建一个查询以显示相关的“报告”。

事件内容与报告的唯一联系是电路。


当我创建'报告'时,我必须为其分配'事件'。但是,当我在报告之前创建“事件”时,我必须为其分配一个“电路”。

例如,它看起来像这样...

> 'report' - Second place for Pedrosa as black flag terminates race for Marquez (1023) 
    > 'event' - Australian Grand Prix (662) 
     > 'circuit' - Phillip Island (156) 

所以我想查询“报告”,其中有在特定的“电路”“活动”。


在我的“事件”的single.php,我有电路的ID,但我不知道怎么列出报告说,它们在该电路。

我可以很容易地显示相关的“事件”使用此查询

$related = new WP_Query(array(
    'post_type' => array('motogp-2013','motogp-2014'), 
    'posts_per_page' => -1, 
    'meta_query' => array(
     array(
      'key' => 'event_calendar_circuit', 
      'value' => $circuit, 
      'compare' => '=', 
      'type' => 'NUMERIC' 
     ) 
    )  
)); 

但这不是我想要实现的,我需要出示相关的报告。


reports_race_event元键包含“事件”的ID号,并且“事件”包含其中包含电路ID的元密钥event_calendar_circuit

我的问题是我怎么了“报告”,当我唯一meta_key是reports_race_event

我需要我们正处在一个特定$circuit


举行列出“报告”做到这一点

如果任何人可以帮助我,那将是非常感谢。


我已经想通了如何做到这一点!

但我仍然需要帮助,请,我可以列出这样的所有相关报表...

$related_reports = new WP_Query(array(
    'post_type' => 'reports', 
    'posts_per_page' => -1, 
    'meta_query' => array(
     array(
      'key' => 'reports_race_event', 
      'value' => $events_at_circuit, 
      'compare' => not sure, 
      'type' => not sure 
     ) 
    )  
)); 

但我需要创建“事件” ID号的数组并将其存储在这个$events_at_circuit变量。

购买使用该查询第一...

global $circuit; 

$related_events = get_posts(array(
    'post_type' => array('motogp-2013','motogp-2014'), 
    'posts_per_page' => -1, 
    'meta_query' => array(
     array(
      'key' => 'event_calendar_circuit', 
      'value' => $circuit, 
      'compare' => '=', 
      'type' => 'NUMERIC' 
     ) 
    )  
)); 

我的问题是,现在,我如何才能从$ related_events查询返回的文章ID号码,并将其存储为一个数组中$events_at_circuit

回答

0

我终于到了那里...

这里是答案,我不得不存储数组中的事件数组,然后在那个电路中,然后使用数组变量查询meta_key值。然后比较使用word的meta IN比较。请参阅下面的完整代码...

$events_at_circuit = null; 

$related_events = get_posts(array(
    'post_type' => array('motogp-2013','motogp-2014'), 
    'posts_per_page' => -1, 
    'meta_query' => array(
     array(
      'key' => 'event_calendar_circuit', 
      'value' => $circuit, 
      'compare' => '=', 
      'type' => 'NUMERIC' 
     ) 
    )  
)); 

if(!empty($related_events)){ foreach($related_events as $related_event) $events_at_circuit[] = $related_event->ID; } 

$related_reports = new WP_Query(array(
    'post_type' => 'reports', 
    'posts_per_page' => -1, 
    'meta_query' => array(
     array(
      'key' => 'reports_race_event', 
      'value' => $events_at_circuit, 
      'compare' => 'IN', 
      'type' => 'NUMERIC' 
     ) 
    )  
)); 

if ($related_reports->have_posts()) :