如何在与另一个自定义类型帖子建立连接时获取自定义类型的帖子数据?

问题描述 投票:0回答:2

我有两种自定义帖子类型:国家和城市。

国家

只有一个文本字段可以填写其名称。

enter image description here

我有一个城市名称的文本字段,还有一个选择,其中包含在其他自定义帖子类型中添加的所有国家/地区。

enter image description here

我的问题是,如何获得每个城市,自己的国家?

要检索我正在使用的城市:

$args = array (
  'post_type' => 'cities'
);
$query = new WP_Query($args);

在这个城市的结果中,我没有任何与我在管理区域中选择的国家/地区相关的信息。

谢谢!

wordpress custom-post-type advanced-custom-fields
2个回答
1
投票

如果您使用的是高级自定义字段,请尝试以下方法 -

<?php 
    $args = array (
      'post_type' => 'cities'
    );
    $query = new WP_Query($args);

    if ( $query->have_posts() ):
        while ( $query->have_posts() ): $query->the_post();
            the_title();
            $country = get_field('countries', get_the_ID());
            echo $country->post_title;
        endwhile;
    endif;
?>

1
投票

如果城市和国家/地区只是单个字段内容文本类型:在邮政类型国家/地区,创建country_cat的自定义分类,然后将city设置为country_cat的子类别。

您可以获得子类别的父级,如此帖子所示:https://wordpress.stackexchange.com/questions/11267/check-is-category-parent-or-not-from-its-id

© www.soinside.com 2019 - 2024. All rights reserved.