Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting returned wordpress fields from WP_Query or 'get' functions

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

like image 514
sneexz Avatar asked Oct 17 '14 08:10

sneexz


4 Answers

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

like image 161
Marqas Avatar answered Oct 18 '22 04:10

Marqas


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.

like image 34
rnevius Avatar answered Oct 18 '22 05:10

rnevius


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
like image 2
Justin L Avatar answered Oct 18 '22 05:10

Justin L


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;
    }
like image 1
jer0dh Avatar answered Oct 18 '22 04:10

jer0dh