Home Forums General Issues Get field values used in custom posts


Get field values used in custom posts

  • Hi,

    I’m building a filter function and I’m looking for a way to echo the field values for a post type.

    I have an archive page with a custom loop that gives me all posts. All these posts have a select field called “year” where you choose which year with radiobuttons.
    Now to filter through them I would like to get all those years from the posts but only once.

    The way I’ve done it now is to create a custom query for just getting the years but it of course returns the year for every post. I just want one of each year used in this post type (2019, 2020, 2021 and so forth).

    This is my code right now:

        $args = array (
          'post_type' => 'match',
          'post_status' => 'publish',
          'posts_per_page' => -1,
        $loop = new WP_Query($args);
        if ( $loop->have_posts() ) { ?>
          <?php while ( $loop->have_posts() ) { $loop->the_post(); ?>
            <?php $field = get_field_object('year');?>
            <?php echo $field['value']; ?></p>
          <?php } ?>
        <?php } ?>
        <?php wp_reset_postdata(); ?>
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.