查询Pods.io以获取相关记录

时间:2015-09-01 14:10:06

标签: wordpress podscms

这会让我绝对疯狂。我有使用Pods的自定义内容类型,我试图获取基于父级的所有相关记录。在这种情况下,我有州和郡。​​

我从:

开始
$states = pods( 'state', $params);

果然我进入了所有州。接下来,我想循环遍历所有状态,并通过'state_id'获取与状态相关的县,但不知道如何做到这一点。这就是我所拥有的。

while ( $states->fetch() ) {
    echo $states->display( 'state_name' ) . "<br />";
    $params = [
        'where'=>"meta_value.state_id = 19"
    ];
    $county = pods( 'county', $params);
    print_r($county);
}

当然,where子句是错误的,但文档最多令人困惑。我看到'related_field_name.meta_value'或'related_field_name.field_name'之类的东西。我尝试了上面的查询,其中where子句有state.meta_value=19(与县相关的州ID),它只给了我一个巨大的信息列表,但没有记录。

在wordpress数据库中,我有wp_postmeta,它包含一些meta_key和meta_value用于县记录,包括一个'state_id'的meta_key,值为'19',我想在循环中匹配。

我怎么能说'抓住所有状态为19的帖子记录'?

1 个答案:

答案 0 :(得分:0)

试试这个:

<?php
$params = array(
    'limit' => -1,
);

$states = pods( 'state', $params );

while ( $states->fetch() ) {
    // Pods::index() maps to "post_title"
    echo '<h3>' . $states->index() . '</h3>';

    // Pods::id() maps to "ID"
    $county_params = array(
        'where' => 'state_id.meta_value = ' . (int) $states->id(),
    );

    /*
    // If you had a relationship field instead
    // and the field was named "state"
    // and it was related to the "state" post type
    // then it would be:
    $county_params = array(
        'where' => 'state.ID = ' . (int) $states->id(),
    );
    */

    $county = pods( 'county', $county_params );

    // List all counties for state
    if ( 0 < $county->total() ) {
        echo '<ul>';

        while ( $county->fetch() ) {
            // index() maps to "post_title"
            echo '<li>' . $county->index() . '</li>';
        }

        echo '</ul>';
    }
}