Ask your WordPress questions! Pay money and get answers fast! (more info)

How to show post by Select Field ACF WordPress

  • SOLVED

Dear friends

I did use ACF with Select Fields and creat field name "hotel_in".
List of selection for "hotel_in" with Label

list1 : List 1
list2 : list 2
list3 : list 3

How to show all post in current category the same list with label before post:

List 1
-post1
-post 2

List 2
-post3
-post4

List 3
-post5
-post6

many thank for support.

Answers (1)

2014-05-09

Bob answers:

[[LINK href="http://www.advancedcustomfields.com/resources/how-to/how-to-query-posts-filtered-by-custom-field-values/"]]http://www.advancedcustomfields.com/resources/how-to/how-to-query-posts-filtered-by-custom-field-values/[[/LINK]]


qdungeng comments:

http://www.advancedcustomfields.com/resources/how-to/how-to-query-posts-filtered-by-custom-field-values/

Example code for only one select field, I need to show post with all label on current category.

List 1
-post1
-post 2

List 2
-post3
-post4

List 3
-post5
-post6

Thanks for support


Bob comments:

May be this work notice you <strong>have to change $field_key variable's value</strong>


$field_key = "field_52a087a80a4c6";
$field = get_field_object($field_key);

if( $field )
{
echo '<div class="list-posts">';
foreach( $field['choices'] as $k => $v )
{

// args
$args = array(
'numberposts' => -1,
'post_type' => 'post',
'meta_key' => $k,
'meta_value' => $v
);

// get results
$the_query = new WP_Query( $args );

// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
<h4><?php echo $v; ?></h4>
<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 wp_reset_query(); // Restore global post data stomped by the_post().


}
echo '</div>';
}


qdungeng comments:

I do not know why your code not working,


$field_key = "field_52a087a80a4c6";
$field = get_field_object($field_key);



if( $field )

{

echo '<div class="list-posts">';

foreach( $field['choices'] as $k => $v )

{



// args

$args = array(

'numberposts' => -1,

'post_type' => 'post',

'meta_key' => $k,

'meta_value' => $v

);



// get results

$the_query = new WP_Query( $args );



// The Loop

?>

<?php if( $the_query->have_posts() ): ?>

<h4><?php echo $v; ?></h4>

<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 wp_reset_query(); // Restore global post data stomped by the_post().





}

echo '</div>';

}


Bob comments:

have you changed value of $field_key variable ?
$field_key = "field_52a087a80a4c6";


Bob comments:

do you get any error? what is your field key?


qdungeng comments:

Hi Bob V

my field key : field_532a6a53dfb20

I did test your code but is not working ?

Please help me.


Bob comments:

can I have access to your ftp? and wordpress admin access to check code?


Bob comments:

Are you using custom post type?


qdungeng comments:

No friend, I did not use custom post type.


Bob comments:

Here is updated code try it

$field_key = "field_52a087a80a4c6";
$field = get_field_object($field_key);
if( $field )
{
echo '<div class="list-posts">';
foreach( $field['choices'] as $k => $v )
{


// args

$args = array(

'numberposts' => -1,

'post_type' => 'post',

'meta_key' => 'hotel_in',

'meta_value' => $v

);



// get results

$the_query = new WP_Query( $args );



// The Loop

?>

<?php if( $the_query->have_posts() ): ?>

<h4><?php echo $v; ?></h4>

<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 wp_reset_query(); // Restore global post data stomped by the_post().





}

echo '</div>';

}


Bob comments:

Updated code notice your key added and "hotel_in" is added instead of $k

$field_key = "field_532a6a53dfb20";
$field = get_field_object($field_key);
if( $field )
{
echo '<div class="list-posts">';
foreach( $field['choices'] as $k => $v )
{

// args
$args = array(
'numberposts' => -1,
'post_type' => 'post',
'meta_key' => 'hotel_in',
'meta_value' => $v
);

// get results
$the_query = new WP_Query( $args );

// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
<h4><?php echo $v; ?></h4>
<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 wp_reset_query(); // Restore global post data stomped by the_post().
}
echo '</div>';
}


Bob comments:

if above code do not work try changing $v with $k in above code so $arg will be

$args = array(
'numberposts' => -1,
'post_type' => 'post',
'meta_key' => 'hotel_in',
'meta_value' => $k
);


qdungeng comments:

$args = array(

'numberposts' => -1,

'post_type' => 'post',

'meta_key' => 'hotel_in',

'meta_value' => $k

);

this code working fine, thank so much. I did add you via Skype, my skype : qdungeng