Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 = "
        $wpdb->posts.ID,
        $wpdb->posts.post_title,
        $wpdb->posts.post_excerpt,
        $wpdb->posts.comment_count,
        $wpdb->posts.guid,
        $wpdb->posts.post_status,
        $wpdb->posts.post_name,
        $wpdb->posts.post_date,
        $wpdb->posts.post_type
    ";
    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
add comment

2 Answers

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
add comment

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
add comment

Your Answer

 
discard

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.