Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

ACF - Multiple choice and Post_Type Issue in List WordPress

  • SOLVED

I'm trying to make table database with the help of already created custom fields with the help of ACF. I've two custom post types namely "Products" & "Company".
"Products" contains Post Object which set to "post_type" to "Company".

For some reason this post object is not working along with the other custom field which contains multiple choice

<?php
$args = array(
'post_type' => 'product_search',
'post_status' => 'publish',
'posts_per_page' => '-1'
);
$products_loop = new WP_Query( $args );
if ( $products_loop->have_posts() ) :
while ( $products_loop->have_posts() ) : $products_loop->the_post();
// Set variables
$title = get_the_title();
$company = get_field('company');
?>

</ul>

<tr>
<td id="1">
<?php
$field_name2 = "product_category";
$field2 = get_field_object($field_name2);
echo '<h4 class="'.$field_name2.'">' . $field2['value'] . '</h4>' ;
?>
</td>

<td id="2">


<?php
if( $company ): // override $post
$post = $company;
setup_postdata( $post );
?>

<h4 class="company_name">
<a rel="<?php the_ID(); ?>" class="ajax" rel="<?php the_ID(); ?>" href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h4>


<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>

</td>

<td id="3">
<?php
if( $company ): // override $post
$post = $company;
setup_postdata( $post );
?>

<h4 class="company_name">
<a rel="<?php the_ID(); ?>" class="ajax" rel="<?php the_ID(); ?>" href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h4>


<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
</td>





<td id="4"><?php
$field_name4 = "regulatory_compliance";
$field4 = get_field_object($field_name4);
$cats = get_field('regulatory_compliance');
echo '<h3>' . $field4['label'] . '</h3>';
echo '<h4 class="'.$field_name4.'">';
foreach ($cats as $key => $val){

echo $val. '<span class="comma">,</span>';

}
echo '</h4>';
$string = rtrim($val,',');
echo '</h3>';

?></td>


</tr>


<?php endwhile; wp_reset_postdata();
endif; ?>




ID's

#1 - Working Fine
#2 - Working Fine
#3 - Displaying Current Page Link
#4 - Warning: Invalid argument supplied for foreach()

Answers (2)

2016-01-15

Romel Apuya answers:

<td id="4"><?php

$field_name4 = "regulatory_compliance";

$field4 = get_field_object($field_name4);

$cats = get_field('regulatory_compliance');

echo '<h3>' . $field4['label'] . '</h3>';

echo '<h4 class="'.$field_name4.'">';

foreach ($cats as $key){



echo $key. '<span class="comma">,</span>';



}

echo '</h4>';

$string = rtrim($val,',');

echo '</h3>';




remove the => $val
?></td>


Shoeb mirza comments:

It is displaying this error again
<blockquote>Warning: Invalid argument supplied for foreach()</blockquote>


Romel Apuya comments:

whats the var_dump of $cats?

2016-01-16

Andrea P answers:

My first guess could be that when you reset_postdata() you are also resetting the $products_loop.

you shouldn't setup_postdata($post) and then reset_postdata(), and rather do something like:

- retrieve the post ID in the field 'company' (instead of the post object)
- then use the ID to get the title and permalink etc.. by instance if $company was actually the ID, you could do:
echo get_permalink($company);
echo get_the_title($company);


when you reset_postdata() the current post_vars is set back to the current page (it quits your custom loop). so when you use get_field() it searches for that field into the current page, and not into the product which was being looped previously in the first part of your code. that's why it fails


Shoeb mirza comments:

<blockquote>when you reset_postdata() the current post_vars is set back to the current page (it quits your custom loop). so when you use get_field() it searches for that field into the current page, and not into the product which was being looped previously in the first part of your code. that's why it fails</blockquote>

:thumbs up:

<?php echo get_permalink($company);
echo get_the_title($company);
?>


:thumbs up:

Great! Thanks!