Enter your keyword

MySQL

Comment Like and Dislike Function in Wordpess

// Create Table in your Databse SQL
CREATE TABLE custom_comment_likes(
    id INT NOT NULL AUTO_INCREMENT,
    comment_id INT NOT NULL,
    ip_address VARCHAR (255),
    status INT NOT NULL,
    PRIMARY KEY (id)
);
function dsweb_comment_like_dislike(){
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    global $wpdb;
    $like_count = $wpdb->get_var( "SELECT * FROM custom_comment_likes WHERE `ip_address` = '".$ip."' AND `comment_id` = '".$_POST[id]."'");
    $count = $wpdb->num_rows;
    if(!$count > 0){
        $wpdb->insert( 'custom_comment_likes',  array(  'ip_address' => $ip, 'comment_id' => $_POST[id] , 'status' => $_POST[status]),  array( '%s', '%d' ,'%d') );
    }else{
        $wpdb->update('custom_comment_likes', array( 'status' => $_POST[status]), array( 'comment_id' => $_POST[id] ,'ip_address' => $ip), array('%d' ),array( '%d','%s' ) );
    }
    $like_query = $wpdb->get_results("SELECT * FROM custom_comment_likes WHERE `comment_id` = '".$_POST[id]."' AND `status` = '1'");
    $like = $wpdb->num_rows;
    $dislike_query = $wpdb->get_results("SELECT * FROM custom_comment_likes WHERE `comment_id` = '".$_POST[id]."' AND `status` = '0'");
    $dislike = $wpdb->num_rows;
    $arg['success']['like'] = $like;
    $arg['success']['dislike'] = $dislike;
    return $arg;
}

Dynamic Copyright Date in WordPress Footer

function wp_copyright() { 
global $wpdb;
$copyright_dates = $wpdb->get_results("SELECT YEAR(min(post_date_gmt)) AS firstdate, YEAR(max(post_date_gmt)) AS lastdate  FROM $wpdb->posts WHERE post_status = 'publish'  "); 
$output = ''; 
if($copyright_dates){ 
$copyright = "© " .$copyright_dates[0]->firstdate; 
if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) { 
$copyright .= '-' . $copyright_dates[0]->lastdate;
}
$output = $copyright;
}
return $output;
}
echo wp_copyright();

Search / Filter posts on Title/Content/Taxonomy and Tags

<?php 
$searchkeys = 'searchkeyword';
global $wpdb;
$querysearch=" SELECT * FROM $wpdb->posts, $wpdb->term_relationships, $wpdb->term_taxonomy, $wpdb->terms WHERE ($wpdb->terms.name = '$searchkeys' OR $wpdb->posts.post_content LIKE '%$searchkeys%' OR $wpdb->posts.post_title LIKE '%$searchkeys%') AND $wpdb->posts.post_type = 'post' AND $wpdb->posts.ID = $wpdb->term_relationships.object_id AND  $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id AND $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id ORDER BY $wpdb->posts.post_date DESC";

$get_results = $wpdb->get_results($querysearch, OBJECT_K);
?>

How to Change Wordpress Site Location

Paste the query in database SQL server.

1.  Update wp_options table.

UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldsite.com', 'http://www.newsite.com') WHERE option_name = 'home' OR option_name = 'siteurl';

2.  Update wp_posts table set guid.

UPDATE wp_posts SET guid = replace(guid, 'http://www.oldsite.com', 'http://www.newsite.com');

3.  Update wp_posts table set post_content.

UPDATE wp_posts SET post_content = replace(post_content, 'http://www.oldsite.com', 'http://www.newsite.com');

Update Term count by SQL

Update WordPress Taxonomies (Categories/Tags) count field after bulk Import/Delete

UPDATE wp_term_taxonomy SET count = (
SELECT COUNT(*) FROM wp_term_relationships rel
LEFT JOIN wp_posts po ON (po.ID = rel.object_id)
WHERE
rel.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND
wp_term_taxonomy.taxonomy NOT IN ('link_category')
AND
po.post_status IN ('publish', 'future')
)