0

So I'm trying to save the value of a custom term to my database. The table is created as is the custom term. The code below outputs the text field onto my custom terms page but it just doesn't save any value to the database.

Can anyone see if there's anything wrong with this? (I'm sure there's something I've missed)

add_action ( 'movie_edit_form_fields', 'movie_access_metabox' );
add_action ( 'edited_movie', 'save_movie_data' );

function movie_access_metabox($movie) {
global $wpdb;
$value = $wpdb->get_var('select meta_value from wp_termmeta where meta_value='.$movie->meta_value);

?>
<tr>
    <th scope="row" valign="top"><label><?php _e('Movie year') ?></label></th>
    <td>
        <input type="text" id="year" name="year" value="" <?php echo $value ?>> 
    </td>
</tr>

<?php
}

function save_movie_data($term_id) {
global $wpdb;
$wpdb->termmeta = 'wp_termmeta';
if (isset($_POST['year'])) {

    $wpdb->delete('wp_termmeta', array('meta_key' => '_year', 'term_id' => $term_id));
    $wpdb->insert('wp_termmeta', array('term_id' => $term_id, ',meta_key' => '_year', 'meta_value' => $value));
}else{
    $wpdb->delete('wp_termmeta', array('meta_key' => '_year', 'term_id' => $term_id));
    }
}

UPDATE #1:

Failing to manage to get the answer provided by @Oleg Butuzov to work, I have put some more time into my original answer and got it to the stage where it saves the users input to the db. The problem now is that it only seems able to save one value, the value can be updated but only one row can be added at a time. If anyone has any ideas please bring them forward and I will continue to update the code so hopefully can help someone else in future.

UPDATE #2:

*Removed wrong code from post.

It turns out that the main reasons I was only able to add one value was due to the fact I had only used delete and insert - no update and had not used AUTO_INCREMENT on the create termmeta schema. Full working answer is provided below.

0

short exmaple how i do it... (tables naming not really correct.)

in this example i am trying to save _description $_POST variable.

add_action ( 'edited_term', 'custom_edited_term', 10, 3);
function custom_edited_term($term_id, $tt_id, $taxonomy){
    if ( defined('DOING_AJAX') || defined('DOING_CRON') )
            return;

    $_POST = stripslashes_deep($_POST); 
    if (isset($_POST['_description']) && trim($_POST['_description']) != ''){
        update_term_meta($term_id, '_description', trim($_POST['_description']));
    } else {
        delete_term_meta($term_id, '_description');
    }
}

Terms Meta Functions

if (!function_exists('add_term_meta')){
    function add_term_meta($term_id, $meta_key, $meta_value, $unique = false) {
        if ( $the_term = wp_is_term_revision($term_id) )
            $term_id = $the_term;
        return add_metadata('term', $term_id, $meta_key, $meta_value, $unique);
    }
}


if (!function_exists('delete_term_meta')){
    function delete_term_meta($term_id, $meta_key, $meta_value = '') {
        return delete_metadata('term', $term_id, $meta_key, $meta_value);
    }
}

if (!function_exists('get_term_meta')){
    function get_term_meta($term_id, $key, $single = false) {
        return get_metadata('term', $term_id, $key, $single);
    }       
}



if (!function_exists('update_term_meta')){
    function update_term_meta($term_id, $meta_key, $meta_value, $prev_value = false) {
        return update_metadata('term', $term_id, $meta_key, $meta_value, $prev_value);
    }
}

terms tables declaration (you can put it to some init function or functions.php - up to you)

global $wpdb;
$wpdb->tables[] = 'termmeta';
$wpdb->termmeta = $wpdb->prefix.'termmeta';

table itself

$wpdb->query("CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}termmeta` (
    `meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `term_id` bigint(20) unsigned NOT NULL DEFAULT '0',
    `meta_key` varchar(255) DEFAULT NULL,
    `meta_value` longtext,
    PRIMARY KEY (`meta_id`),
    KEY `meta_key` (`meta_key`),
    KEY `term_id` (`term_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1");
| improve this answer | |
  • no joy unfortunately. Do you think it could be a problem with where I'm trying to hook the action onto? – user657489 Jun 12 '15 at 13:20
  • Sorry, my fault. I miss that we talk about term not post type (thanks for *metabox function name). I am using edited_term hook to save meta for terms. I will udpate my answer in few minutes. – Oleg Butuzov Jun 12 '15 at 13:31
  • Thanks a lot for that! So is the function 'update_term_meta' where the saving happens? I will work through this now and update once I'm done. Thanks again. – user657489 Jun 12 '15 at 13:49
  • Maybe i'm missing something, but it still does not work for me. I've added a simple text input to refer to description variable and nothing is getting saved to the database. Thanks for all your help anyway. – user657489 Jun 15 '15 at 14:32
0

Here's my final code which answers my question. It allows the user to input a value through a text field which is then either added, replaced, or deleted from the database (if user types 'delete'). It also gets the value and displays it in the input field if there is one.

function movie_edit_add_year($movie){
  global $wpdb;
  $v = $wpdb->get_var("SELECT meta_value FROM wp_termmeta where term_id =".$movie->term_id);
function has_meta_value($p){
    return isset($p) ? $p : '';
}
?>
<tr>
    <th><label>Movie year</label></th>
    <td>
        <input type="text" name="year" style="width:5%;" value="<?php echo has_meta_value($v); ?>" >
    </td>
</tr> <?php
}

add_action( 'edited_movie', 'save_year_to_movie');

function save_year_to_movie( $term_id ) {
   global $wpdb;
   $table = $wpdb->prefix . 'termmeta';
   $val = intval($_POST['year']);
   $var = $wpdb->get_var("SELECT meta_id FROM wp_termmeta where term_id=".$term_id);

if ($_POST['year'] == 'delete') {
    $wpdb->delete($table, array('term_id' => $term_id));
} elseif (!empty($var)) {
    $wpdb->update($table, array('term_id' => $term_id, 'meta_key' => 'year', 'meta_value' => $val),
        array('term_id' => $term_id), array('%d', '%s', '%d') );
} else {
    $wpdb->insert($table, array('term_id' => $term_id, 'meta_key' => 'year', 'meta_value' => $val),
        array('%d', '%s', '%d'));
}
}
| improve this answer | |

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.