0

As the title says I want Advanced Custom Fields Plugin to create a Select field which populated with current categories on the site. The exact use case is that the admin can select one category from the list of categories showed on a Select. Then based on that selected category, most 5 recent posts from that category shows on the home page of the site. But this question is only specific to the First Step; Show site's categories in Select Field dynamically.

Many thanks.

1
  • 1
    ACF has a taxonomy field that does this for you.
    – Milo
    May 1 '17 at 4:27
2

Set the "Return Value" of the taxonomy to Term ID. This will return the term/category id so that when you get the ACF taxonomy value by using get_field, it will return the id of your category. Then by using following code you can show your 5 latest publish post under that category.

$cat_id = get_field('<your ACF field name>');
$args = array(
  'post_type'     => 'post', //or your postype 
  'post_status'   => 'publish',
  'posts_per_page' => 5,
  'cat' => cat_id 
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) : 
    while ( $query->have_posts() ) : $query->the_post(); 
        the_title();
        the_excerpt();
    endwhile;
endif;
wp_reset_postdata(); 
1
  • Thanks @Aishan , You Saved me from asking another question!
    – startToday
    May 1 '17 at 8:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.