Wordpress mod rewrite / wordpress slug


add_rewrite_rule

add_action('init', 'wa_add_init');
function wa_add_init()
{
    ob_start();
    add_rewrite_rule('testimonial/([^/]+)/?', 'index.php?pagename=testimonial&testimonial_slug=$matches[1]', 'top');
    add_rewrite_rule('project/([^/]+)/?', 'index.php?pagename=project&project_slug=$matches[1]', 'top');
    add_rewrite_rule('work-detail/([^/]+)/?', 'index.php?pagename=work-detail&work_slug=$matches[1]', 'top');
    //flush_rewrite_rules();
}

add_filter( 'query_vars', array( $this, 'query_vars_func'));
function query_vars_func( $query_vars )
{
    $query_vars[] = 'project_category_slug';
    $query_vars[] = 'project_slug';
    $query_vars[] = 'case_study_slug';
    return $query_vars;
}


function getIdBySlug($table_name, $slug)
{
    global $wpdb;
    $query = "SELECT ID FROM " . $table_name . " WHERE slug='" . $slug . "'";
    $id = (int)$wpdb->get_var($query);
    return $id;
}

$slug = get_query_var('work_slug');
$id = getIdBySlug($wpdb->prefix."works", $slug);