繁体   English   中英

WordPress 通过 ACF 关系字段查询

[英]WordPress query by ACF relationship field

我有一个自定义帖子类型“建筑”和另一个自定义帖子类型“建筑师”。 建筑通过 ACF 关系字段与建筑师相关联。

在单一建筑师页面中,我想显示与该特定建筑师相关联的建筑物列表。

到目前为止,我已通过执行以下操作实现了这一目标:

$loop = new WP_Query( array( 'post_type' => 'building', 'paged' => $paged, 'posts_per_page' => -1 ) );
if ( $loop->have_posts() ) : ?>

    <ul>

        <?php while ( $loop->have_posts() ) : $loop->the_post();

            $building_name = get_the_title(); 
            $building_link = get_the_permalink();

            $architects = get_field('architect'); 
            if( $architects ): ?>
                <?php foreach( $architects as $post):?>
                    <?php setup_postdata($post);
                    if( get_the_title() == $architect_name ) { ?>
                        <li><a href="<?php echo $building_link; ?>"><?php echo $building_name ?></a></li>
                    <?php } ?>
                <?php endforeach; ?>

                <?php wp_reset_postdata(); ?>
            <?php endif; ?>

        <?php endwhile; ?>

    </ul>

<?php endif;
wp_reset_postdata();

然而,这似乎不是很有效,我希望将关系引入查询本身,我已经尝试过这样做:

$buildings = get_posts(array(
    'post_type' => 'building',
    'meta_query' => array(
        array(
            'key' => 'architect',
            'value' => '"' . get_the_ID() . '"',
            'compare' => 'LIKE'
        )
    )
));

<?php if( $buildings ): ?>
    <ul>
        <?php foreach( $buildings as $building): ?>

            <li><a href="<?php echo get_permalink( $building->ID ); ?>"><?php echo get_the_title( $building->ID ); ?></a></li>

        <?php endforeach; ?>
    </ul>
<?php endif; ?>

哪个不起作用,它不返回任何东西......

你能看出我做错了什么,或者你有任何其他想法来处理这种情况吗?

这样的事情应该在单一的架构师页面上进行(因为你已经在循环中只显示那个架构师而你需要得到建筑物)......基于ACF文档。

<?php 

$posts = get_field('building');

if( $posts ): ?>
    <ul>
    <?php foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT) ?>
        <li>
            <a href="<?php echo get_permalink( $p->ID ); ?>"><?php echo get_the_title( $p->ID ); ?></a>
            <span>Custom field from $post: <?php the_field('author', $p->ID); ?></span>
        </li>
    <?php endforeach; ?>
    </ul>
<?php endif; ?>

如果你基于查询,那就更像了

<?php 
$ids = get_field('building', false, false);

$query = new WP_Query(array(
    'post_type'         => 'architect',
    'posts_per_page'    => -1,
    'post__in'          => $ids,
    'post_status'       => 'any',
    'orderby'           => 'post__in',
));

?>

实际上,这很大程度上取决于你建立关系的方式。 您是否在建筑师页面上显示自定义字段并在那里选择建筑物?

我通过更改 meta_query 中的值解决了它。

由此 :

'"' . get_the_ID() . '"'

到 :

get_the_ID()

这是我目前使用的解决方案。 我看起来很结实。

$buildings = new WP_Query(array(
    'post_type'        => 'building',
    'posts_per_page'   => -1,
    'suppress_filters' => 0,
    'meta_query'       => array(
            array(
                'key'      => 'architect',
                'value'    => '"'.$architect_id.'"',
                'compare'  => 'LIKE'
            )
        ),
        'order' => 'ASC',
        'orderby' => 'title',
        'post_status' => 'publish',
    )); 

while ( $buildings->have_posts() ) {
    $buildings->the_post();

    // print title, content, or whatever here.

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM