【问题标题】:php query based on relational child elements for wordpress loop基于关系子元素的wordpress循环的php查询
【发布时间】:2014-01-19 13:00:24
【问题描述】:

我会尽量简单解释一下。

我有 3 种帖子类型在这里发挥作用。

  1. 报告
  2. 事件(motogp-2013、motogp-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 号,而“事件”包含元键 event_calendar_circuit 包含电路 ID。

我的问题是,当我拥有的唯一元密钥是 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

【问题讨论】:

    标签: php wordpress meta


    【解决方案1】:

    我终于到了那里......

    这就是答案,我必须存储该电路中事件的数组编号,然后使用数组变量通过 meta_key 值进行查询。然后使用 wordpress 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() ) :
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 2020-05-13
      • 2011-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多