I would like to show values from custom fields in my Post Categories in Wordpress. To do this, I have used some code in my functions.php
file to add and save these custom fields to the different categories in the Wordpress database. I am running into an issue, when trying to pull the values from the custom fields.
Here's the issue: I am trying to use get_term_meta
to pull the term_id
variable on WordPress:
echo get_term_meta($term_id, 'site_section_1', true);
However, the variable is not showing up when I do this, but it works when I call an individual $term_id
such as:
echo get_term_meta(47, 'site_section_1', true);
My database table looks something like this:
meta id term_id meta_key meta_value
1 47 site_section_1 Blog Category
2 47 site_subsection_1 Events Stories
3 47 department_1 Events
4 48 site_section_1 Blog Category
5 48 site_subsection_1 Communications Stories
6 48 department_1 Communications
I have added this code to the functions.php
file to add custom fields to Post Categories:
function wcr_category_fields($term) {
if (current_filter() == 'category_edit_form_fields') {
$site_section_1 = get_term_meta($term->term_id, 'site_section_1', true);
$site_subsection_1 = get_term_meta($term->term_id, 'site_subsection_1', true);
$department_1 = get_term_meta($term->term_id, 'department_1', true);
?>
<tr class="form-field">
<th valign="top" scope="row"><label for="term_fields[site_section_1]"><?php _e('Site Section'); ?></label></th>
<td>
<input type="text" size="40" value="<?php echo esc_attr($site_section_1); ?>" id="term_fields[site_section_1]" name="term_fields[site_section_1]"><br/>
</td>
</tr>
<tr class="form-field">
<th valign="top" scope="row"><label for="term_fields[site_subsection_1]"><?php _e('Site Subsection'); ?></label></th>
<td>
<input type="text" size="40" value="<?php echo esc_attr($site_subsection_1); ?>" id="term_fields[site_subsection_1]" name="term_fields[site_subsection_1]"><br/>
</td>
</tr>
<tr class="form-field">
<th valign="top" scope="row"><label for="term_fields[department_1]"><?php _e('Department'); ?></label></th>
<td>
<input type="text" size="40" value="<?php echo esc_attr($department_1); ?>" id="term_fields[department_1]" name="term_fields[department_1]"><br/>
</td>
</tr>
<?php } elseif (current_filter() == 'category_add_form_fields') {
?>
<div class="form-field">
<label for="term_fields[site_section_1]"><?php _e('site_section_1'); ?></label>
<input type="text" size="40" value="" id="term_fields[site_section_1]" name="term_fields[site_section_1]">
</div>
<div class="form-field">
<label for="term_fields[site_subsection_1]"><?php _e('site_subsection_1'); ?></label>
<input type="text" size="40" value="" id="term_fields[site_subsection_1]" name="term_fields[site_subsection_1]">
</div>
<div class="form-field">
<label for="term_fields[department_1]"><?php _e('department_1'); ?></label>
<input type="text" size="40" value="" id="term_fields[department_1]" name="term_fields[department_1]">
</div>
<?php
}
}
add_action('category_add_form_fields', 'wcr_category_fields', 10, 2);
add_action('category_edit_form_fields', 'wcr_category_fields', 10, 2);
function wcr_save_category_fields($term_id) {
if (!isset($_POST['term_fields'])) {
return;
}
foreach ($_POST['term_fields'] as $key => $value) {
update_term_meta($term_id, $key, sanitize_text_field($value));
}
}
add_action('edited_category', 'wcr_save_category_fields', 10, 2);
add_action('create_category', 'wcr_save_category_fields', 10, 2);
Please, let me know, if you need any other information.