WordPress.org

Ready to get started?Download WordPress

Forums

get post from slect field ACF (6 posts)

  1. qdungeng
    Member
    Posted 7 months 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.
    Thanks

  2. Steven Jones
    Member
    Posted 6 months ago #

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

  3. qdungeng
    Member
    Posted 3 months ago #

    How to show all post from current category example :

    Hotel in Hanoi
    -post1
    -post 2

    Hotel in Saigon
    -post3
    -post4

    Hotel in Hue City
    -post5
    -post6

    Thanks

  4. bcworkz
    Member
    Posted 3 months ago #

  5. qdungeng
    Member
    Posted 3 months ago #

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

    <?php
    $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 ];
    ?>
    
    <?php
    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() ): ?>
    	<ul>
    	<?php  while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		<li>
    
     <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    		</li>
    	<?php endwhile; ?>
    	</ul>
    <?php endif; ?>
    
    <?php
    endwhile;
    endif;
    wp_reset_query();
    ?>

    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
    3.Hanoi
    post 1
    post 2

    Why is Hanoi display two times .
    Please help me .

    Thanks

  6. bcworkz
    Member
    Posted 3 months 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().

Reply

You must log in to post.

About this Topic

Tags

No tags yet.