for example in php when you read a file per line you can use fopen("inputfile.txt"); but this puts all lines in an array and cause memory error when the file is very big, an alternative to it is the fopen("inputfile.txt", "r"); and fgets(); you can read a file per line from disk, without loaded all to memory.

I was wondering if there is something similar with Wp query to searching the database 'slowly' without waiting to hit the memory limit even if the query is more slow to be done.

here is the query:

$arr = array();
$args = array(
    'post_type' => 'post',
    'order'             => 'ASC',
    'posts_per_page' => '-1',
$query = new WP_Query($args);

    while ($query->have_posts()) : $query->the_post();
        if(get_post_meta( get_the_ID(),'slider_postition',true )!='None') {
            $pos = get_field('slider_postition', get_the_ID());
            $arr[$pos-1] = get_the_ID();

for($i = 0;$i<15;$i++) {
   if($arr[$i] > 0) {
  • edit your question to explain which query you want to optimize. then we can help you to find a way of splitting the query
    – mmm
    Jan 14, 2018 at 21:57
  • okk i updated the post with the query Jan 14, 2018 at 22:46

1 Answer 1


It appears that you're trying to get the latest 15 posts where the meta key is not equal to 'None'.

Instead of loading all the posts into memory and then looping through to see if the meta value is not 'None', you could use the meta_query parameter.

$args = [
  'post_type'      => 'post',
  'orderby'        => 'date',
  'order'          => 'ASC',
  'posts_per_page' => '15',
  'meta_query'     => [
      'key'     => 'slider_position',
      'value'   => [ 'None' ],
      'compare' => '!=',
$query = new WP_Query( $args );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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