简体   繁体   中英

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.

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. 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. 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).

• I've tried wrapping that tag_id in '', echoing it, etc. But it just displays nothing.

• 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?

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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