WordPress: Faster WP_Query with preselected fields

Speaks many languages, but currently only uses PHP, JavaScript and WordPress (I consider it its own language, since it's a big sandbox).
Search for a command to run...

Speaks many languages, but currently only uses PHP, JavaScript and WordPress (I consider it its own language, since it's a big sandbox).
No comments yet. Be the first to comment.
Recently, a friend asked me to send a script to remove pre tags from a string, including content. He uses a script to calculate the reading time for the content, but doesn't want the pre-tags to be included in the scoring. So if you ever have a simil...

When there are problems with one's WordPress website, it happens that one has to create a user without having access to the database or the WordPress admin. Then it is useful to be able to create or log in a user using FTP. So here you will learn how...

Occasionally it happens that overlaps occur in a large table and the message Duplicate entry '0' for key 'PRIMARY' for query then gets the upper hand in the error logs. This error can be fixed quite easily with a short line of SQL. Fix duplicate entr...

WordPress is packed with useful features. It is easy to lose the overview. Here you will find a selection of functions that deal with arrays and objects. A small collection of useful functions that you can always use, if you know that they exist. wp_...

As a rule, searches are created with the MySQL LIKE. This usually works very well, as long as the database table is not too large. However, if you work with a large database (millions+ entries), then you quickly notice how long a search in it actuall...

I had already written something about it on my website, but since I find this little "trick" very useful and it has helped me from many "slow" rides, I share it here today.
In WordPress you can create your own queries using the class WP_Query(). The problem with these queries is that if they are used frequently, they are very slow, because WordPress loads all fields of the database there.
To explicitly get IDs, you can put the following into the arguments of the query, so that only IDs are loaded:
$args = [
'post_type' => 'my-post-type',
'posts_per_page' => -1,
'fields' => 'ids', // select IDs only
];
$query = new WP_Query( $args );
print_r( $query->posts );
// [ 1, 2, 3, 4, 5 ]
Here only the IDs are loaded. This saves a lot of time, even if it looks like more effort. This method is also very helpful if you want to combine several queries.
Remember that if you use get_the_title() or similar functions, WordPress will automatically load the entire post with get_post() again. Instead, you can load the full query again and save yourself the extra effort. You can also use get_post() only once in the loop and then retrieve the content:
$args = [
'post_type' => 'my-post-type',
'posts_per_page' => -1,
'fields' => 'ids', // select IDs only
];
$query = new WP_Query( $args );
if( $query->found_posts ):
foreach( $query->posts as $post_id ):
$post = get_post($post_id);
print $post->post_title;
endforeach;
endif;
How do you build your queries?