Support

Account

Home Forums ACF PRO Taxonomy to Find Category Selected

Solved

Taxonomy to Find Category Selected

  • Hi,

    I’m attempting to create a query to find any Case Studies (a Custom Post Type) that are selected via the page. This is my first time using the Taxonomy field, and although it works, I get the error in Debug mode “Warning: Invalid argument supplied for foreach() in…”. I’ve followed a few suggestions to fix that, but they don’t show any results.

    My Field Page

    
    <img src="https://postimg.cc/D4GNVRYQ" alt="" />
    

    My Code

    
    <?php
      foreach($case_study_category as $cs) { $cs = get_category($cs); }
      $args = array(
        'post_type' => 'case-study',
        'posts_per_page' => 9,
        'post_status' => 'publish',
        'orderby' => 'publish_date',
        'category__in' => array($case_study_category),
      );
    
      $study_query = new WP_Query( $args);
    
      if ( $study_query->have_posts() ) { 
    ?>
      <div class="post-grid post-grid--scroll-mob">
    <?php
      while ( $study_query->have_posts() ) {
        $study_query->the_post();
    
        $name_title = get_field('name_title');
    ?>
      <a>" class="post-block f-rounded-bottom-lg mob-center-fix">
      
      <?php if(has_post_thumbnail()) {
        the_post_thumbnail('case-study', array('class' => 'post-image post-image--featured', 'alt' => $name_title));
       } else {
      ?>
        <img src="https://placehold.co/1232x924" />
      <?php } ?>
        <span class="post-block__text box-pad-xs d-block">
          <h5 class="font-inter color-black weight-600 text-scale-sm mb-0"><?php echo the_title(); ?></h5>
          <?php if( ($name_title) ) : ?><h6 class="post-sub-title font-quicksand color-blue text-uppercase weight-600 mb-0 text-scale-mini"><?php echo $name_title; ?></h6>
        </span><?php endif; ?>
      </a>
    <?php
      }
    ?>
      </div>
    <?php
        wp_reset_postdata();
      } else {
    ?>
      <p class="color-brown">There are currently no Case Studies to show associated with this service.</p>
    <?php
      }
    ?>
    
  • As there’s no option to preview a post before submitting, and strangely no option to Edit a post once approved, this topic is pretty useless due to the way the formatting has been output and the image isn’t showing either for reasons I’m not sure of.

    I can’t even delete it and create a new version by the looks.

  • Since this is your only foreach I have to assume this is what’s causing the error

    
    foreach($case_study_category as $cs) { $cs = get_category($cs); }
    

    How are you populating the variable $case_study_category?

  • Ah yes the block is wrapped inside a

    
    <?php 
      $case_study_category = get_field('case_study_category');
    			
      if( ($case_study_category) ) :
    ?>
    ...
    <?php endif; ?>
    

    The field is Taxonomy (Category)
    Return Value: Term ID

    Screen here of field page: https://postimg.cc/D4GNVRYQ

  • it is a radio field and may be returning only a single term, not an array. In this case you would not need a foreach loop.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.