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

Currently I am making a custom design for a site. The problem I'm seeing in Wordpress is about almost all data pulled out from the database.

While in certain pages - like front-page.php - it only needs, for example, title, category, two metas and the excerpt, WP gets everything. Hell, the post content is only visible in post-single.php, but instead WP pulls out all the content, tags, guid, tags, etc. where it can (print_r helps a lot).

The question is about how to limit Wordpress data pulled out from his queries. I think that I should make a net "WP_Query" (or override it) based on $wpdb and use it for the site, instead of using the default one. Well, at least functions like query_posts, WP_Query and get_posts doesn't allow me to limit the data pulled out.

I am also interested in caching the data, because is really small, but this is first.

Update 1: Tried to "minimize data":

    add_action( 'pre_get_posts', 'disable_main_query' );
function disable_main_query( $query ) {
    if( $query->is_main_query() && $query->is_front_page() ) {
        add_filter('posts_fields', 'minimized_data');

function minimized_data($fields) {
    global $wpdb;
    $fields = "
    return $fields;

Mow the default loop is plagued with "Undefined property: stdClass::$post_XXXX" and other things. I think the_post is to blame now, so I'm cheking.

Update 2: Has expected, it was. Changed the while loop for foreach ($posts as $post) no more undefined properties, but as earlier I can't get rid of ob_end_flush() [ref.outcontrol]: failed to delete buffer zlib output compression.

Update 3: define( 'SAVEQUERIES', FALSE ); and no more failed buffer (for WAMP).

share|improve this question
Well, i'm seeing that the load time between $wpdb, WP_query and get_posts() doesn't vary too much - everyone with the same arguments. [30ms~] – DarkGhostHunter Jun 15 '12 at 22:05
up vote 1 down vote accepted

You can filter 'posts_fields', check if it is the main query and limit the queried fields to … whatever you need.
See WP_Query::get_posts() in /wp-includes/query.php for details and side effects.

For debugging the queries I recommend the plugin Debug Bar. If you add …

define( 'WP_DEBUG',         TRUE );
define( 'SAVEQUERIES',      TRUE );

… to your wp-config.php you can see the resulting query and how it affects later queries.

share|improve this answer
Thanks, I will try to use post_fields. That is, the most undocumented filter of Wordpress. – DarkGhostHunter Jun 16 '12 at 0:45
Updated. It seems that excluding data makes the_post() go crazy (undefined variables). – DarkGhostHunter Jun 16 '12 at 3:12

After some digging, it appears that the only way to "get only what you need" is by using de class $wpdb to talk with the database. That's mean also a second loop, outside the main one.

Now, for performance sake, there should be a way to modify the main/default loop that Wordpress always does to NOT get the post content and another useless data (for the site).

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.