【问题标题】:Use ACF Taxonomy Field as Variable in Wordpress Custom Post Type Loop在 Wordpress 自定义帖子类型循环中使用 ACF 分类字段作为变量
【发布时间】:2020-05-21 00:03:24
【问题描述】:

我正在尝试创建自定义 Wordpress 查询,使用高级自定义字段字段作为循环中的变量。

用例是一个页面上有一个自定义循环。例如,关于场地的页面在页面底部显示事件循环(自定义帖子类型)。我希望创建页面的人选择他们想要附加到页面的事件标签(CPT 上的标签分类)。然后循环运行,该字段附加到用作变量的标记查询。

到目前为止,这是我的代码:

            <?php if ( get_field('event_tag') ) : ?>

            <?php 
                $event_tag = get_field('event_tag');
                $args = array(
                    'post_type' => 'events',
                    'posts_per_page' => 3,
                    'tag_id' => $event_tag,
                    'meta_key' => 'event_start_date',
                    'orderby' => 'meta_value',
                    'order' => 'ASC',
                    'ignore_sticky_posts' => true
                );
            ?>
            <?php echo $event_tag; ?><!-- This is only here to check the variable -->

        <?php else : ?>

            <?php 
                $args = array(
                    'post_type' => 'events',
                    'posts_per_page' => 3,
                    'meta_key' => 'event_start_date',
                    'orderby' => 'meta_value',
                    'order' => 'ASC',
                    'ignore_sticky_posts' => true
                );
            ?>

        <?php endif; ?>

            <?php $secondquery = new WP_Query( $args ); 
                if ( $secondquery->have_posts() ) : while ( $secondquery->have_posts() ) : $secondquery->the_post(); ?>

我仍然想按事件日期排序,因此是 meta_key 和 orderby。我不确定这是否会影响这一点。需要注意的几点:

• 回显$event_tag 的临时行确实输出了标签ID(在本例中为31)。

• 我已尝试将 tag_id 包装在 '' 中、回显它等。但它什么也没显示。

• 因为这是查询自定义帖子类型,所以我不确定标准标签是否有效。标签是这样注册的……如果重要的话。

    // Taxonomy / Tags
function taxonomies_events_tags() {
  $args = array(
    'hierarchical'  => false, 
    'label'         => __( 'Event Tags','taxonomy general name'), 
    'singular_name' => __( 'Event Tag', 'taxonomy general name' ), 
    'rewrite'       => true, 
    'query_var'     => true,
    'show_in_rest' => true
  );
  register_taxonomy( 'custom-tag', 'events', $args );
}
add_action( 'init', 'taxonomies_events_tags', 0 );

我需要在查询中进行哪些更改才能显示指定标记中的事件,仍然按 event_start_date 排序?

提前致谢。

【问题讨论】:

    标签: wordpress wordpress-theming advanced-custom-fields custom-post-type custom-taxonomy


    【解决方案1】:

    您需要使用税务查询来获取特定类别的事件。假设 $event_tag 变量包含分类术语的标签 id,下面的代码应该可以工作:

    $args = array(
      'post_type' => 'events',
      'posts_per_page' => 3,
      'meta_key' => 'event_start_date',
      'orderby' => 'meta_value',
      'order' => 'ASC',
      'ignore_sticky_posts' => true,
      'tax_query' => array(
        array(
          'taxonomy' => 'custom-tag',
          'field'    => 'term_id',
          'terms'    => $event_tag
        )
      )
    );
    

    【讨论】:

    • 成功了!我想我很困惑,因为分类不是查询所在页面的一部分。但这是我查询的帖子的一部分。代码太多,咖啡不够。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2020-06-11
    • 2011-03-21
    • 1970-01-01
    • 2020-06-15
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多