Support » Plugins » Hacks » get post from slect field ACF

  • Dear friend

    I did install ACF (advanced Custom fields) and Create Select Field “Hotel_in”, and more choices :
    SG : Saigon
    HN : Hanoi
    HC : Hue City

    Please help me how to get all post title and URL from single field and get label from single choices .

    I did use :

    if(get_field(‘hotel_in’) == “HG”)

    But it not working. Please help me.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The get_field() is case sensitive so looks like you need to capitalise the H in Hotel.

    How to show all post from current category example :

    Hotel in Hanoi
    -post 2

    Hotel in Saigon

    Hotel in Hue City


    Moderator bcworkz


    Thanks for your support
    but it not working fine : Please help me to edit this code :

    $category = get_the_category($post->ID);
    $category = $category[0]->cat_ID;
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    $field = get_field_object('hotel_in');
    $value = get_field('hotel_in');
    $label = $field['choices'][ $value ];
    echo '<h2>Hotel in ' .$label. '</h2>';
    $args = array(
    	'post_type' => 'post',
    	'posts_per_page' => -1,
    	'meta_key' => 'hotel_in',
    	'meta_value' => $value,
    	'cat' => $category
    $the_query = new WP_Query( $args );
    <?php if( $the_query->have_posts() ): ?>
    	<?php  while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
     <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    	<?php endwhile; ?>
    <?php endif; ?>

    I did post any post and select field : HN,SG
    but it load the same :

    1.Hanoi :
    post 1
    post 2
    2. Sai gon :
    post 2
    post 3
    post 1
    post 2

    Why is Hanoi display two times .
    Please help me .


    Moderator bcworkz


    There’s some problem with your outer query. You need to call wp_reset_postdata() when the inner loop finishes. That may or may not solve the repeat in the outer query. Since you did not provide the argument and query code for the outer loop, there may be other issues.

    The first two lines are suspicious to me. You only use $post->ID inside a loop, yet these are outside the two loops I see. Is there a third loop? Or is the category code misplaced?

    It appears, by hard coding the ‘meta_key’ argument (‘hotel_in’) instead of using $field, that you solved the trim() parameter error you were getting from your other topic. If the hard coded argument will always be correct for any condition, this will be preferable to using get_field_object().

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘get post from slect field ACF’ is closed to new replies.