Support

Account

Home Forums Front-end Issues Reverse Query ACF Term "meta" Reply To: Reverse Query ACF Term "meta"

  • Hi @joelstransky

    ACF save the taxonomies’ value in the wp_options table. In this case, you need to use the wpdb class.

    Here’s an example how to do it:

    // you can set the ID automatically. This is just an example
    $location_id = 99;
    
    // this is the taxonomy b's slug
    $taxonomy_slug = 'taxonomy_b';
    
    // this is the taxonomy b's parent field name
    $taxonomy_field_name = 'location_field_name';
    
    $rows = $wpdb->get_results($wpdb->prepare( 
        "
        SELECT option_name 
        FROM {$wpdb->prefix}options
        WHERE option_name LIKE %s
        AND option_value = %s
        ",
        $taxonomy_slug . '_%_' . $taxonomy_field_name,
        $location_id
    ));
    
    $unit_ids = array();
    
    foreach( $rows as $row ){
        preg_match('/^' . $taxonomy_slug . '_(\d*)_' . $taxonomy_field_name . '$/', $row->option_name, $matches);
        $unit_ids[] = $matches[1];
    }
    
    $terms = get_terms(array(
        'taxonomy' => $taxonomy_slug,
        'hide_empty' => false,
        'include' => $unit_ids,
    ));
    
    print_r($terms);

    I hope this helps 🙂