1

I'm working on a modification to a custom taxonomy that would allow content managers to specify a numeric order in which the terms shall appear in a widget. They can't rely on the alphabetical order in this case. I've used this tutorial to add an input[type=number] to the entry and edit forms, and that's working fine. The problem occurs when actually adding or editing a term in order to save the order. Here's the code:

public function sanitize_term_order($number) {
    return is_numeric($number) ? absint($number) : 1;
}

public function add_order_to_service_hierarchy_when_adding() {
    $field = $this->controller->hierarchy_tax . "-term-order"; ?>

    <div class="form-field form-field-custom term-group <?php echo $field ?>">
        <label for="<?php echo $field ?>">Term Order</label>
        <input type="number" step="1" min="1" name="<?php echo $field ?>" id="<?php echo $field ?>" value="1">
        <?php wp_nonce_field(basename(__FILE__), $field . "-nonce") ?>
    </div>

<?php }

public function add_order_to_service_hierarchy_when_editing($term) {
    $field = $this->controller->hierarchy_tax . "-term-order";
    $value = get_term_meta($term->term_id, $field, true);

    if (!is_numeric($value)) {
        $value = 1;
    } ?>

    <tr class="form-field form-field-custom term-group-wrap <?php echo $field ?>">
        <th scope="row"><label for="<?php echo $field ?>">Term Order</label></th>
        <td>
            <input type="number" step="1" min="1" name="<?php echo $field ?>" id="<?php echo $field ?>" value="<?php echo $value ?>">
            <?php wp_nonce_field(basename(__FILE__), $field . "-nonce") ?>
        </td>
    </tr>

<?php }

public function save_hierarchy_term_order($term_id) {
    $field = $this->controller->hierarchy_tax . "-term-order";
    if (isset($_POST[$field . "-nonce"]) && wp_verify_nonce($_POST[$field . "-nonce"], basename(__FILE__))) {
        $value = $this->sanitize_term_order($_POST[$field]);

        echo ("field: $field<br>value: $value<br>term_id: $term_id<br><br>");
        $results = update_term_meta((int)$term_id, $field, $value);
        die(var_dump($results));
    }
}

And here's an example of what the results look like:

field: service-hierarchy-term-order
value: 2
term id: 58

bool(false) 

I've added filters and actions to every hook that exists in both taxonomy.php and meta.php that I could find that is called after I used update_term_meta() in my code. I've tried using add_term_meta() and update_term_meta() separately just to see if that would help. I've changed the field's name to see if there was a hidden length restriction despite VARCHAR(255) being the length of the meta_key column in the database.

And, no luck. For now, just so that I can continue development, I've solved the problem using site options (i.e. update_option($field ."-". $term_id, $value)) which will work, but I'd prefer to figure out why the term meta function is failing.

Any thoughts?

  • seems like it should work, just a guess but maybe it worked the first time and you didn't notice, and now is returning false because there is no change? if you change to die(var_dump(get_term_meta($term_id, $field, true))); does it give you a value? – majick Mar 10 '16 at 17:03
  • Unfortunately, that doesn't seem to be the case because the $value = get_term_meta($term->term_id, $field, true); code in the edit method isn't changing as I test things. But, I'll dig in to double-check considering I didn't realize that update_term_meta would return false if there was no change. Thanks! – David Kees Mar 10 '16 at 19:08
  • @majick - unfortunately, get_term_meta is also returning bool(false). – David Kees Mar 10 '16 at 20:57
  • all I can suggest is just keep digging into what might return false if you follow the function through to update_metadata... eg. a longshot but test the table is okay with _get_meta_table('term'); – majick Mar 11 '16 at 2:18
  • Yeah, the table's name is returned, but I'm beginning to wonder if there is a problem at the database level. Unfortunately, I don't have access to the database itself, but I'll get in touch with those who do. I'm also going to try it on a different, fresh installation of WP to see if that changes things. Thanks for taking a look. – David Kees Mar 11 '16 at 13:30
0

This turned out to be a database issue.

Due to some security measures put in place by the infrastructure team, the network upgrade script was prevented from completing the upgrade on all sites. Thus, for the specific site within our WordPress Network on which this plugin was being tested, the wp_termmeta table had not been created. We worked with our team to undo what they'd done, re-ran the network upgrade, and all is well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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