custom post types - minimize wp_query call to database

admin2025-06-06  2

I have a very basic question which can be done easily with raw sql query where instead of * we can name the columns of database we want output like

      select id,name,date from tableA where id>'100'

but same thing in Wp_Query is giving whole lot of junk data which is completely irrelevant to us. We can filter those data with php but the problem is it will definitely kill database as it is making lot of calls behind the scene.

We checked with var_dump and it was huge data 99% of not useful to us. We want to list only id and name column not columns like PING_STATUS,COMMENT_STATUS,MENU_ORDER,COMMENT_STATUS etc

 while ( $has_more_images ) {

    $args = array(
        'posts_per_page' => '100',
        'offset'         => $offset,
        'post_type'      => 'attachment',
        'post_status'    => 'any',
        'orderby'        =>'ID',
        'order'          => 'ASC',
        'no_found_rows'  => true


    );


    $the_query = new WP_Query( $args );
    var_dump( $the_query );

 }

I have a very basic question which can be done easily with raw sql query where instead of * we can name the columns of database we want output like

      select id,name,date from tableA where id>'100'

but same thing in Wp_Query is giving whole lot of junk data which is completely irrelevant to us. We can filter those data with php but the problem is it will definitely kill database as it is making lot of calls behind the scene.

We checked with var_dump and it was huge data 99% of not useful to us. We want to list only id and name column not columns like PING_STATUS,COMMENT_STATUS,MENU_ORDER,COMMENT_STATUS etc

 while ( $has_more_images ) {

    $args = array(
        'posts_per_page' => '100',
        'offset'         => $offset,
        'post_type'      => 'attachment',
        'post_status'    => 'any',
        'orderby'        =>'ID',
        'order'          => 'ASC',
        'no_found_rows'  => true


    );


    $the_query = new WP_Query( $args );
    var_dump( $the_query );

 }
Share Improve this question edited Nov 7, 2018 at 8:34 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Nov 7, 2018 at 8:23 SteeveSteeve 1032 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

WP_Query is not a general-purpose method for querying the database. It's specifically designed for querying posts with the intention to loop over them and output templates for them.

If you just want arbitrary data from the wp_posts table, then you can just write SQL and use the $wpdb object to run it:

$query       = $wpdb->prepare( "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'attachment' ORDER BY ID LIMIT 100 OFFSET %d", $offset );
$attachments = $wpdb->get_results( $query );

if ( ! empty( $attachments ) ) {
    foreach ( $attachments as $attachment ) {
        $id   = $attachment->ID;
        $name = $attachment->post_title;
    }
}

This uses the $wpdb->prepare() method to safely add the $offset variable to the SQL query, and then uses $wpdb->get_results() method to run that query and return an array of objects for each result. The $wpdb->posts part of the query allows you to target the wp_posts table regardless of what the database prefix might be (Users can configure it to be something other than wp_, and on multisite each site has a different posts table).

Keep in mind that the purpose of the wp_posts table, and the attachment post type, is for that data to behave like posts, where things like the status and date are important. If you're trying to use posts for something that isn't like a 'post' then you probably shouldn't be using the posts table.

You seem to want to list the name of everything in the Media Library, and I'm honestly not sure why you'd want to do that, it's quite an odd thing to want to do. So if this isn't your actual use-case I'd recommend updating your question with whatever it actually is.

This looks like a simple question, but it touches upon a complex matter of optimization that affects all computing, from chipsets and low level compilers to higher level systems like WordPress. Any hardware or software that serves a general purpose, must compromise in the way it handles requests. That means that every single request could probably be handled more efficiently if the system was purposely build for it.

So, in your case, you conclude that wp_query translates to SQL in an inefficient way for your particular purpose. That is only to be expected, given the above. If you know which SQL query you want to run, the most efficient way to go is to simply pass that query through wp_query, and bypass any parsing by WP itself. Like this:

$sql = "SELECT SQL ... the rest of your query";
$query = new WP_Query();
$query->parse_query($sql);
$posts = $query->get_posts();  

A lesser known feature of WP is that you can use filters to change the SQL query that wp_query would normally generate. You can use these filters to change the default compromises WP makes while parsing queries. You would use these if you will be running a specific type of queries which you want to optimize. But, once more, if you know exactly which SQL you want to run, just bypass WP's parsing.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749200402a317205.html

最新回复(0)