与ACF分类字段查询WP定制的帖子

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

我需要查询与ACT分类字段自定义信息。我创造了这个代码:

我的分类ACF领域返回期限对象,并允许多选但是这个代码仅适用于最后所选词语。我需要查询所有选择项。

<?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 ),
        ),
    ),
    )
    ); 
    ?>

哪里是我的错?

wordpress advanced-custom-fields
1个回答
0
投票

你正在做的所有条款的一个foreach,然后设置的foreach这当然只在最后一个学期带给因为你每次都重写前值内查询的值。既然是多选,你应该能够整张专辑阵列传递给WP_Query。此外,请确保您在返回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,
      ),
    ),
  ),
); ?>
© www.soinside.com 2019 - 2024. All rights reserved.