get post from slect field ACF (6 posts)

  1. qdungeng
    Posted 1 year ago #

    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.

  2. Steven Jones
    Posted 1 year ago #

    The get_field() is case sensitive so looks like you need to capitalise the H in Hotel.

  3. qdungeng
    Posted 1 year ago #

    How to show all post from current category example :

    Hotel in Hanoi
    -post 2

    Hotel in Saigon

    Hotel in Hue City


  4. bcworkz
    Posted 1 year ago #

  5. qdungeng
    Posted 1 year ago #

    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 .


  6. bcworkz
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.