简体   繁体   English

在 Wordpress 自定义帖子类型循环中使用 ACF 分类字段作为变量

[英]Use ACF Taxonomy Field as Variable in Wordpress Custom Post Type Loop

I'm attempting to create a custom Wordpress query, using an Advanced Custom Fields field as a variable in the loop.我正在尝试创建一个自定义 Wordpress 查询,使用高级自定义字段字段作为循环中的变量。

The use case is a page has a custom loop on it.用例是一个页面上有一个自定义循环。 For example, a page about a venue displays a loop of events (the custom post type) at the bottom of the page.例如,有关场地的页面会在页面底部显示事件循环(自定义帖子类型)。 I want for the person creating the page to choose what event tag (a tag taxonomy on the CPT) they want to attach to the page.我希望创建页面的人选择他们想要附加到页面的事件标签(CPT 上的标签分类法)。 Then the loop runs with that field attaching to the tag query used as a variable.然后循环运行,该字段附加到用作变量的标记查询。

Here's my code so far:到目前为止,这是我的代码:

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

I'm still wanting to sort by the event date, thus the meta_key and orderby.我仍然想按事件日期排序,即 meta_key 和 orderby。 I'm not sure if that's affecting this.我不确定这是否会影响这个。 A couple things to note:需要注意的几点:

• That temporary line echoing the $event_tag does output the tag id (in this case 31). • 与 $event_tag 相呼应的临时行确实输出了标签 ID(在本例中为 31)。

• I've tried wrapping that tag_id in '', echoing it, etc. But it just displays nothing. • 我曾尝试将 tag_id 包装在 '' 中,对其进行回显等。但它什么也没显示。

• Because this is querying a custom post type, I'm not sure if the standard tag even works. • 因为这是查询自定义帖子类型,所以我不确定标准标签是否有效。 The tag is registered like this...if it matters.标签是这样注册的……如果重要的话。

    // 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 );

What do I need to change in my query to get the events in the specified tag to show, still ordered by event_start_date?我需要在查询中更改什么才能显示指定标记中的事件,但仍按 event_start_date 排序?

Thanks in advance.提前致谢。

You need to use a tax query to get events from a certain category.您需要使用税务查询来获取某个类别的事件。 Assuming the $event_tag variable contains the tag id for the taxonomy term, the following piece of code should work:假设 $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
    )
  )
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM