提问者:小点点

按ACF关系字段进行WordPress查询


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

在single architect页面中,我想显示与该特定架构师关联的建筑列表。

到目前为止,我通过以下方式实现了这一目标:

$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; ?>

这不起作用,它不会返回任何东西。。。

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


共3个答案

匿名用户

类似的东西应该在single architect页面上完成(因为您已经在一个循环中只显示该架构师,并且您需要获得建筑)。。。基于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',
));

?>

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

匿名用户

我通过更改meta_查询中的值解决了这个问题。

由此:

'"' . 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.

}