1

I am attempting to autogenerate the terms in a custom taxonomy from a front end field. However, when I use the following code and try to type in a letter the dropdown menu that appears is a bunch of black boxes (screenshot below). Any help would be appreciated. Please let me know if you need any more info. Thanks!

<?php 
$args = array(
    'orderby'                => 'name',
    'order'                  => 'ASC',
    'hide_empty'             => false,
    'include'                => array(),
    'exclude'                => array(),
    'exclude_tree'           => array(),
    'number'                 => '',
    'offset'                 => '',
    'fields'                 => 'all',
    'name'                   => '',
    'slug'                   => '',
    'hierarchical'           => true,
    'search'                 => '',
    'name__like'             => '',
    'description__like'      => '',
    'pad_counts'             => false,
    'get'                    => '',
    'parent'                 => '0',
    'childless'              => false,
    'cache_domain'           => 'core',
    'update_term_meta_cache' => true,
    'meta_query'             => ''
);
$phpArray = get_terms( 'listing_taxonomy', $args );
?>
<script>
    $(function() {
    var availableTags = <?php echo json_encode($phpArray); ?>;
    $( "#tags" ).autocomplete({
      source: availableTags
    });
    });
</script>

enter image description here

Calling console.log(availableTags) gives the following: enter image description here

0

1 Answer 1

1

Hi the autocomplete expects source to be array, URL string and function return. Even if you give JSON then it should have label and value. Which is not present here as you are simply output the get_terms with all fields.

So what I would suggest is just return the name of categories. Changing the fields from all to names will work well.

<?php 
$args = array(
    'orderby'                => 'name',
    'order'                  => 'ASC',
    'hide_empty'             => false,
    'include'                => array(),
    'exclude'                => array(),
    'exclude_tree'           => array(),
    'number'                 => '',
    'offset'                 => '',
    'fields'                 => 'names',
    'name'                   => '',
    'slug'                   => '',
    'hierarchical'           => true,
    'search'                 => '',
    'name__like'             => '',
    'description__like'      => '',
    'pad_counts'             => false,
    'get'                    => '',
    'parent'                 => '0',
    'childless'              => false,
    'cache_domain'           => 'core',
    'update_term_meta_cache' => true,
    'meta_query'             => ''
);
$phpArray = get_terms( 'listing_taxonomy', $args );
?>
<script>
    $(function() {
    var availableTags = <?php echo json_encode($phpArray); ?>;
    $( "#tags" ).autocomplete({
      source: availableTags
    });
    });
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.