7

Looking to limit the returned fields of a WP Query to help with speeding up the response from the server and reducing the amount of data retrieved. For the query I'm using, it only needs up to 3 fields of data, the rest is brought in through ACF get_field_object in the loop. Other functions I'm using such as get_posts or get_terms have field options but are limited to a small number of things, such as 'slug' only or 'id => slug'.

I'm used to developing in CakePHP, which has the option to specify each and every field to return, but the project calls for wordpress for other functionality and so I'm quite limited.

TL;DR need to speed up getting posts from Wordpress

ndm
  • 59,784
  • 9
  • 71
  • 110
sneexz
  • 265
  • 2
  • 4
  • 10
  • What are the fields? Are they all custom? – rnevius Oct 17 '14 at 08:17
  • The fields I want looking in to limiting are the ones that come as default, like 'post_content', 'guid', 'post_modified' etc. – sneexz Oct 17 '14 at 08:20
  • possible duplicate of [Fetch Selected Fields in WP\_Query Class in WordPress](http://stackoverflow.com/questions/16559326/fetch-selected-fields-in-wp-query-class-in-wordpress) – rnevius Oct 17 '14 at 08:41
  • I may be asking the same question as you quoted above, but the answer provided for him doesn't match my needs. His answered provided a way to access 'title' and 'description' after they were returned by the WP_Query. On the other hand, I'm looking for specifically removing all other data from being returned in the query, other than a select few. With the intention of reducing the amount of data being sent back. – sneexz Oct 17 '14 at 08:51
  • You have 3 return options: 'all ids, id=>parent' Developer doc: https://developer.wordpress.org/reference/classes/wp_query/#return-fields-parameter – gtamborero Jan 05 '22 at 10:52

5 Answers5

3

I used fields parameter in the query and run get posts on this query. For example: In my case, I just needed to get the Post ids for multiple categories, so I created a query like this:

$the_query = new WP_Query( array( 
                        'ignore_sticky_posts' => 1,
                        'posts_per_page'      => -1,
                        'cat'                 => '2,6,7' ,
                        'fields'              => 'ids',
                        'post_type'           => 'post',
                        'post_status'         => 'publish', 
                                ) 
                        );

Run the get_posts on this query:

$posts = $the_query->get_posts();

$posts will get only the IDs of particular categories posts.

Or it can also be done with the standard and popular way and i.e., by running the loop of have_posts:

if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            $post_id_array[] = get_the_ID(); 
        }           
    }

These are the two ways to help with speeding up the response from the server and reducing the amount of data retrieved

Marqas
  • 174
  • 2
  • 4
2

WP_Query will return objects...so it's pretty fast. However, if you really want to limit what's returned, you can do so with the Return Fields Parameter of WP_Query.

rnevius
  • 26,578
  • 10
  • 58
  • 86
  • 3
    I've looked at that, unluckily though it only provides 3 options, which are about 1 or 2 fields short of what I need to retrieve. – sneexz Oct 17 '14 at 08:42
2

This is what I've done to limit the fields from WP_Query, especially, when I want to json_encode them. The $return variable contains my array of posts with only the fields listed in the $fields array.

    $query = new WP_Query( array( 'post_type' => 'my_custom_type' ) );
    $return = array();  
    $fields = array('post_title', 'ID');  //list of fields I want in $return
    $posts = $query->get_posts();
    foreach($posts as $post) {
        $newPost = array();
        foreach($fields as $field) {
            $newPost[$field] = $post->$field;
        }
        $return[] = $newPost;
    }
jer0dh
  • 384
  • 3
  • 6
  • 2
    This doesn't optimise the query, you could easily achieve what you are actually doing with a simple array map – Max Aug 20 '21 at 19:20
2

I don't know how much it will help but below is how I'm getting a flattened array from a CPT. It's not the fastest but it could be worse. I'm using ACF to get a Custom Field but you could just get back the slug or you could get back multiple fields instead:

// Query Jobs Args
$query_args = array(
    'post_type' => 'job',
    'posts_per_page' => -1,
    'fields' => 'ids'
);

// Get Jobs Query
$query = new WP_Query($query_args);

// Loop Persistent Vars
$job_ids = array();

// Loop Over Jobs
foreach($query->posts as $post_id) {
    $job_ids[] = get_field('job_id', $post_id);
}

// Do stuff with flattened array of job ids
Justin
  • 150
  • 2
  • 7
0

Interestingly enough, you can do this with the WP Rest API using the _fields parameter

https://yoursite.com/wp-json/wp/v2/posts?_fields=author,id,excerpt,title,link

More info on the API here: https://developer.wordpress.org/rest-api/

lukeocom
  • 3,243
  • 3
  • 18
  • 30