【问题标题】:List of slugs from custom post taxonomy for loop (Wordpress + ACF Pro)来自自定义帖子分类法循环的 slug 列表(Wordpress + ACF Pro)
【发布时间】:2020-06-15 14:29:51
【问题描述】:

我在最新版本中使用acf prowordpress。 我有一个灵活的内容字段,我想在其中显示某些事件。 显示的事件应由分类字段选择,其中可以通过复选框选择自定义后分类(位置)的多个条目(城市)。 (多选) 返回是一个分类对象。 我需要所选字段中的 slug 列表。 该列表应该是例如比如:“慕尼黑”、“伦敦”、

相关代码是

<?php 
$locations = get_sub_field('select_locations');
$loop = new WP_Query(array(
  'post_type' => 'events', 
  'posts_per_page' => 99,   
  'tax_query' => array(
    array(
      'taxonomy' => 'locations',
      'field' => 'slug',
      'terms' => array(
      /* HERE SHOULD BE THE LIST */
      )
    )
   ) 
  )); 
?>

我尝试了几件事,但无法正常工作。 如果有人能帮我解决这个问题,我会很高兴。

非常感谢。

【问题讨论】:

    标签: wordpress advanced-custom-fields custom-taxonomy


    【解决方案1】:

    试试这个

    $category_slug_arr = array('example-slug-1', 'example-slug-2', 'example-slug-3');
    
    $args = array(
        'posts_per_page'   => -1,
        'orderby'          => 'date',
        'order'            => 'DESC',
        'post_type'        => 'events',
        'post_status'      => 'publish',
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'locations',
                'field'    => 'slug',
                'terms'    => $category_slug_arr,
                'operator' => 'IN'
            )
        )
    );
    
    $loop = new WP_Query( $args );
    
    echo '<pre>'; print_r($loop->posts); echo "</pre>";
    

    【讨论】:

      【解决方案2】:

      感谢您的回答。我在开发人员的帮助下找到了解决方案。

      仅供感兴趣的人使用:

      <?php 
      $locations  = get_sub_field('select_locations');
      $locationsSelected  = array();
      
      foreach( $orte as $ort ):
          $locationsSelected[] = $ort->slug;
      endforeach; 
      
      
      $loop = new WP_Query( array( 'post_type' => 'kurse', 'posts_per_page' => 99, 'meta_key' => 'datum', 'orderby' => 'meta_value', 'order' => 'ASC',
      
              'tax_query' => array(
                  array(
                              'taxonomy' => 'locations',
                              'field' => 'slug',
                              'terms' => $locationsSelected
                          )
              ) 
        ) );  
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-31
        • 2015-10-09
        • 1970-01-01
        • 2019-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-21
        相关资源
        最近更新 更多