Support

Account

Home Forums General Issues How to replace hard-coded list of custom taxonomy terms based on custom field qu

Helping

How to replace hard-coded list of custom taxonomy terms based on custom field qu

  • I am currently hard-coding custom taxonomy terms “13329,5555” into a block of code. Instead of hard-coding the custom taxonomy product_cat term_id as “13329,5555” how would I dynamically replace 13329,5555 with (a comma-delimited representation) of only the product_cat term_ids that have their custom field “product_category_customText” populated? In other words I am looking for help replacing 13329,55555 with a variable representing a comma-separated list including only those product_cat terms that have a value (as opposed to blank) in my custom field “product_category_customText” on the product_cat taxonomy.

    switch ($taxonomy)
            {
                case 'product_cat': return array(13329,5555); break; 
            }

    For what it’s worth below is the code that displays the custom field “product_category_customText” when the user is viewing any product_cat:

    $queried_object = get_queried_object();
        $taxonomy = $queried_object->taxonomy;
        $term_id = $queried_object->term_id;
    
        if( get_field('product_category_customText', $taxonomy . '_'.$term_id)){
            echo '<div id="about_this_category_id">';
            the_field('product_category_customText', $taxonomy . '_' . $term_id);
            echo '</div>';
        }
    

    If someone can please help me replace my hard-coded “13329,5555” with a variable that represents A,Comma,Separated,List of product_cat term IDs where the criteria for building the list is that the product_cat term ids must have the “product_category_customText” field populated, that would be very helpful.

    Thank you!

  • Are you using ACF5?

    If you are then you can use get_terms() with a meta_query https://developer.wordpress.org/reference/functions/get_terms/

    
    <?php 
      
      $args = array(
        'taxonomy' => 'product_cat',
        'meta_query' => array(
          array(
            'key' => 'product_category_customText',
            'value' => '',
            'compare' => '!='
          )
        )
      );
      $terms = get_terms($args);
      
    ?>
    

    If you are using ACF4, then it will be more complicated. ACF4 has not been updated to use term meta. In this case you’d need to get all the terms and then loop through them all to see if they have a value in the field.

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

You must be logged in to reply to this topic.