WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

my single posts is getting 31 query from mysql. I checked all queries. I saw this query.

I am using last posts and categories widget. I think there query is unnecessary.How to remove theme

Query: SELECT option_value FROM xy_options WHERE option_name = 'widget_pages' LIMIT 1
Query: SELECT option_value FROM xy_options WHERE option_name = 'widget_calendar' LIMIT 1
Query: SELECT option_value FROM xy_options WHERE option_name = 'widget_tag_cloud' LIMIT 1
Query: SELECT option_value FROM xy_options WHERE option_name = 'widget_nav_menu' LIMIT 1

my sidebar is here https://gist.github.com/4546327

share|improve this question
Are you sure these Queries are unnecessary? Can you post your single.php? It's impossible to answer this with the given information. – fischi Jan 16 '13 at 10:18
I coluld not add code there correctly. I added to github.gist.github.com/4546327 – ahmetlutfu Jan 16 '13 at 10:54
up vote 2 down vote accepted

Best I can tell is that those default widgets get options from the database as their classes are constructed, so there's no way to prevent the DB queries without disabling those widgets entirely. Depending on your needs, you may want that slight performance bump and do not need the widgets, in which case you can use this code to disable them (place in functions.php):

// Remove unneeded widgets that have undesirable query overhead
add_action( 'widgets_init', 'remove_unneeded_widgets' );
function remove_unneeded_widgets() {

See here for more default widget class names: http://codex.wordpress.org/Function_Reference/unregister_widget


I did some more research and discovered that these four widgets do not have any auto-loaded options, even though several other widgets do have auto-loaded options set in the wp_install_defaults() function in /wp-admin/includes/upgrade.php. This may be an oversight. In any event, if these widgets have never been loaded into a sidebar, the WP_Widget parent class or the widget class have never had the opportunity to set any defaults. The other widgets are constructed and use get_option() but don't produce a query because they're autoloaded with lots of other WordPress options. These four don't have autoloaded options so get_option() has to query the database for each.

The simplest solution is just to assign defaults when the theme is first activated, like so:

// Clean up widget settings that weren't set at installation
add_action( 'after_switch_theme', 'set_missing_widget_options' );
function set_missing_widget_options( ){
    add_option( 'widget_pages', array ( '_multiwidget' => 1 ) );
    add_option( 'widget_calendar', array ( '_multiwidget' => 1 ) );
    add_option( 'widget_tag_cloud', array ( '_multiwidget' => 1 ) );
    add_option( 'widget_nav_menu', array ( '_multiwidget' => 1 ) );
share|improve this answer

From a glance at your Code, your sidebar.php is quite extensive and variable.

The Options are not directly loaded in your sidebar.php, I suppose them to be loaded in your widgets, of which you seem to have a few. As your Theme seems to have a lot of Options available, you need to load all those values to ensure all features.

I strongly recommend not messing with the code in this case, as you could lose features you may later need, and the 4 calls of get_option() that produce the above query should be handled in no time.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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