Update wp_term_relationships table by using the value of wp_postmeta

Hello There I have created a meta box for date in my functions.php of admin. The code is here:-

function ep_eventposts_metaboxes() {
    //add_meta_box( 'ept_event_date_start', 'Start Date and Time', 'ept_event_date', 'event', 'side', 'default', array( 'id' => '_start') );
    add_meta_box( 'ept_event_date_end', 'Expiratory Date and Time', 'ept_event_date', 'post', 'side', 'default', array('id'=>'_end') );
    //add_meta_box( 'ept_event_location', 'Event Location', 'ept_event_location', 'event', 'normal', 'default', array('id'=>'_end') );
}
add_action( 'admin_init', 'ep_eventposts_metaboxes' );

// Metabox HTML

function ept_event_date($post, $args) {
    $metabox_id = $args['args']['id'];
    global $post, $wp_locale;

    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), 'ep_eventposts_nonce' );

    $time_adj = current_time( 'timestamp' );
    $month = get_post_meta( $post->ID, $metabox_id . '_month', true );

    if ( empty( $month ) ) {
        $month = gmdate( 'm', $time_adj );
    }

    $day = get_post_meta( $post->ID, $metabox_id . '_day', true );

    if ( empty( $day ) ) {
        $day = gmdate( 'd', $time_adj );
    }

    $year = get_post_meta( $post->ID, $metabox_id . '_year', true );

    if ( empty( $year ) ) {
        $year = gmdate( 'Y', $time_adj );
    }

    $hour = get_post_meta($post->ID, $metabox_id . '_hour', true);

    if ( empty($hour) ) {
        $hour = gmdate( 'H', $time_adj );
    }

    $min = get_post_meta($post->ID, $metabox_id . '_minute', true);

    if ( empty($min) ) {
        //$min = '00';
        $min = gmdate( 'i', $time_adj );
    }

    $month_s = '<select name="' . $metabox_id . '_month">';
    for ( $i = 1; $i < 13; $i = $i +1 ) {
        $month_s .= "ttt" . '<option value="' . zeroise( $i, 2 ) . '"';
        if ( $i == $month )
            $month_s .= ' selected="selected"';
        $month_s .= '>' . $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) ) . "</option>n";
    }
    $month_s .= '</select>';

    echo $month_s;
    echo '<input type="text" name="' . $metabox_id . '_day" value="' . $day  . '" size="2" maxlength="2" />';
    echo '<input type="text" name="' . $metabox_id . '_year" value="' . $year . '" size="4" maxlength="4" /> @ ';
    echo '<input type="text" name="' . $metabox_id . '_hour" value="' . $hour . '" size="2" maxlength="2"/>:';
    echo '<input type="text" name="' . $metabox_id . '_minute" value="' . $min . '" size="2" maxlength="2" />';

}
/*
function ept_event_location() {
    global $post;
    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), 'ep_eventposts_nonce' );
    // The metabox HTML
    $event_location = get_post_meta( $post->ID, '_event_location', true );
    echo '<label for="_event_location">Location:</label>';
    echo '<input type="text" name="_event_location" value="' . $event_location  . '" />';
}
*/
// Save the Metabox Data

function ep_eventposts_save_meta( $post_id, $post ) {

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;

    if ( !isset( $_POST['ep_eventposts_nonce'] ) )
        return;

    if ( !wp_verify_nonce( $_POST['ep_eventposts_nonce'], plugin_basename( __FILE__ ) ) )
        return;

    // Is the user allowed to edit the post or page?
    if ( !current_user_can( 'edit_post', $post->ID ) )
        return;

    // OK, we're authenticated: we need to find and save the data
    // We'll put it into an array to make it easier to loop though

    $metabox_ids = array('_end' );

    foreach ($metabox_ids as $key ) {

        $aa1 = $_POST[$key . '_year'];
        $mm1 = $_POST[$key . '_month'];
        $jj1 = $_POST[$key . '_day'];
        $hh = $_POST[$key . '_hour'];
        $mn = $_POST[$key . '_minute'];

        $aa1 = ($aa1 <= 0 ) ? date('Y') : $aa1;
        $mm1 = ($mm1 <= 0 ) ? date('n') : $mm1;
        $jj1 = sprintf('%02d',$jj1);
        $jj1 = ($jj1 > 31 ) ? 31 : $jj1;
        $jj1 = ($jj1 <= 0 ) ? date('j') : $jj1;
        $hh = sprintf('%02d',$hh);
        $hh = ($hh > 23 ) ? 23 : $hh;
        $hh = ($hh <= 0 ) ? date('H') : $hh;
        $mn = sprintf('%02d',$mn);
        $mn = ($mn > 59 ) ? 59 : $mn;
        $mn = ($mn <= 0 ) ? date('i') : $mn;

        $events_meta[$key . '_year'] = $aa1;
        $events_meta[$key . '_month'] = $mm1;
        $events_meta[$key . '_day'] = $jj1;
        $events_meta[$key . '_hour'] = $hh;
        $events_meta[$key . '_minute'] = $mn;
        $events_meta[$key . '_eventtimestamp'] = $aa1 ."-" .$mm1 ."-". $jj1." " . $hh.":" . $mn.":".date('s'); 
echo $events_meta;      
        }

    // Add values of $events_meta as custom fields

    foreach ( $events_meta as $key => $value ) { // Cycle through the $events_meta array!
        if ( $post->post_type == 'revision' ) return; // Don't store custom data twice
        $value = implode( ',', (array)$value ); // If $value is an array, make it a CSV (unlikely)
        if ( get_post_meta( $post->ID, $key, FALSE ) ) { // If the custom field already has a value
            update_post_meta( $post->ID, $key, $value );
        } else { // If the custom field doesn't have a value
            add_post_meta( $post->ID, $key, $value );
        }
        if ( !$value ) delete_post_meta( $post->ID, $key ); // Delete if blank
    }

}

add_action( 'save_post', 'ep_eventposts_save_meta', 1, 2 );

/**
 * Helpers to display the date on the front end
 */

// Get the Month Abbreviation

function eventposttype_get_the_month_abbr($month) {
    global $wp_locale;
    for ( $i = 1; $i < 13; $i = $i +1 ) {
                if ( $i == $month )
                    $monthabbr = $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) );
                }
    return $monthabbr;
}

// Display the date

function eventposttype_get_the_event_date() {
    global $post;
    $eventdate = '';
    $month = get_post_meta($post->ID, '_month', true);
    $eventdate = eventposttype_get_the_month_abbr($month);
    $eventdate .= ' ' . get_post_meta($post->ID, '_day', true) . ',';
    $eventdate .= ' ' . get_post_meta($post->ID, '_year', true);
    $eventdate .= ' at ' . get_post_meta($post->ID, '_hour', true);
    $eventdate .= ':' . get_post_meta($post->ID, '_minute', true);

    echo $eventdate;

}

It works fine and adding the metabox on post. And Putting the value in wp_postmeta.
Now i have created a function to change the value add a TaxonomyID for the post after the condition satisfies. The code is here:-

Read More
function wp_insert_category_to_post()
 {
 $postid=get_the_ID();
echo $meta_values = get_post_meta( $postid, '_end_minute', true )."</br>";  
echo $meta_values = get_post_meta( $postid, '_end_hour', true )."</br>"; 
echo $meta_values1 = get_post_meta( $postid, '_end_month', true )."</br>"; 
echo $meta_values2 = get_post_meta( $postid, '_end_day', true )."</br>"; 
echo $meta_values3 = get_post_meta( $postid, '_end_year', true )."</br>";
echo $meta_values3 = get_post_meta( $postid, '_end_eventtimestamp', true )."</br>"; 
    global $wpdb;   
    foreach( $wpdb->get_results("SELECT * FROM wp_postmeta where meta_key='_end_eventtimestamp' And meta_value between '2015-07-01' and now() ;") as $key => $row) {
        // each column in your row will be accessible like this
    $my_column = $row->post_id;
    //$wpdb->insert($wpdb->wp_term_relationships, array("object_id" => $row->post_id, "term_taxonomy_id" => 3, "term_order" => 0), array("%d", %d", "%d"));
    $sql = $wpdb->prepare( "INSERT INTO $wpdb->wp_term_relationships (object_id, term_taxonomy_id,term_order ) VALUES ( %d, %d, %d )", $row->post_id, 3, 0 );
     print_r($sql);
    $res= $wpdb->query($sql);
    //$wpdb->query($sql);
    //echo "<br> My value = ".$my_column."<br>";
}
return $res;
 }
 add_action)('init','wp_insert_category_to_post');

It is written in post.php of wp-admin. But Its not working. can any one tell me the reason. And give the solution.

Related posts