0

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).

1
  • 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~] Commented Jun 15, 2012 at 22:05

2 Answers 2

1

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.

2
  • Thanks, I will try to use post_fields. That is, the most undocumented filter of Wordpress. Commented Jun 16, 2012 at 0:45
  • Updated. It seems that excluding data makes the_post() go crazy (undefined variables). Commented Jun 16, 2012 at 3:12
0

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).

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