Wordpress 使用ACF分类字段查询WP自定义帖子

Wordpress 使用ACF分类字段查询WP自定义帖子,wordpress,advanced-custom-fields,Wordpress,Advanced Custom Fields,我需要使用ACT分类字段查询自定义帖子。我创建了以下代码: My Taxonomy ACF字段返回术语对象并允许multiselect,但此代码仅适用于最后一个选定的术语。我需要查询所有选定的条款 <?php $album_count = get_sub_field('album-count'); $album = get_sub_field('album-custom-category'); ?> <?php foreach ( $album as $album ); ?&g

我需要使用ACT分类字段查询自定义帖子。我创建了以下代码:

My Taxonomy ACF字段返回术语对象并允许multiselect,但此代码仅适用于最后一个选定的术语。我需要查询所有选定的条款

<?php
$album_count = get_sub_field('album-count');
$album = get_sub_field('album-custom-category'); ?>
<?php foreach ( $album as $album ); ?>
<?php $the_query = new WP_Query( 
    array(
    'post_type' => 'gallery',
    'orderby' => 'date',
    'posts_per_page' => 6,
    'tax_query' => array(
        array(
            'taxonomy' => 'albums',
            'field'    => 'slug',
            'terms'    => array( $album->slug ),
        ),
    ),
    )
    ); 
    ?>


我的错误在哪里?

您正在对所有术语进行foreach,然后在foreach中设置查询的值,这当然只会引入最后一个术语,因为您每次都覆盖上一个值。因为它是多选的,所以您应该能够将整个相册数组传递给WP_查询。另外,确保返回的是ACF中的术语ID值,而不是术语对象。然后将代码更新为如下内容:

<?php
$album_count = get_sub_field('album-count');
$albums = get_sub_field('album-custom-category'); ?>

<?php $the_query = new WP_Query( 
  array(
    'post_type' => 'gallery',
    'orderby' => 'date',
    'posts_per_page' => 6,
    'tax_query' => array(
      array(
        'taxonomy' => 'albums',
        'field'    => 'term_id', // This line can be removed since it’s the default. Just wanted to show that you’re passing the term is instead of slug.
        'terms'    => $albums,
      ),
    ),
  ),
); ?>


我以前做过,但它给出了一个错误-网站上此代码下面的所有内容都丢失了:/Ok,我明白了重点-我没有将“slug”改为“term\u id”。现在它是有意义的。谢谢!没问题。很高兴我能帮忙