【问题标题】:Select post with specific group - subfield value选择具有特定组的帖子 - 子字段值
【发布时间】:2020-01-21 21:11:04
【问题描述】:

在 wordpress 中,我有自定义帖子类型“referenzen”。此帖子类型具有自定义字段(ACF)“Referenzen-buildin-type”组,其中子字段“building-type”是复选框。我不知道如何选择具有特定建筑类型的帖子。这对我不起作用:

$posts = get_posts(array(
  'meta_query' => array(
      array(
          'key'     => 'referenzen-building-types_building-type',
          'value'   => '"Museen"',
          'compare' => 'LIKE'
      )
  )
));

有什么想法吗?谢谢

【问题讨论】:

    标签: wordpress advanced-custom-fields


    【解决方案1】:

    请看下面:

    <?php
      //args
      $args = = array (
        'numberposts'   => -1,
        'post_type'     => 'event',
        'meta_key'      => 'location',
        'meta_value'    => 'Melbourne'
      );
    
      //query
      $the_query = new WP_Query( $args );?>
    
    <?php if( $the_query->have_posts() ): ?>
      <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
        /* do something */
      <?php endwhile; ?>
    <?php endif; ?>
    
    <?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>
    

    希望对你有帮助。

    【讨论】:

    • 请不要忘记更改帖子类型名称和其他参数
    • 谢谢,我唯一忘记在 $args 中添加的是帖子类型。可能这是必要的,因为它是自定义帖子类型。使用 'post_type' 它可以工作。
    【解决方案2】:
    $posts = get_posts(array(
      'meta_query' => array(
          array(
              'key'     => 'referenzen_building_types_building_type',
              'value'   => 'Museen',
              'compare' => 'LIKE'
          )
      )
    ));
    

    【讨论】:

    • 谢谢,但这也返回空数组
    • 虽然这段代码 sn-p 可以解决问题,including an explanation 确实有助于提高您的帖子质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要用解释性 cmets 挤满你的代码,因为这会降低代码和解释的可读性!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多