loop – How do I split a large query with a semi-expensive function included into multiple smaller queries

Question

I have a count stored in post-meta called version-count. I just wrote a little plugin to delete the data stored in this post-meta using delete_post_meta_by_key.

I want my plugin to then go through each post and re-run my count-versions function, which saves the new count in post-meta.

The problem is that when I run this code, I get a 504 error. My query returns 8,000+ results.

$args= array(
    'post_type' => 'release',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'release-format',
            'field' => 'slug',
            'terms' => 'album',
        ),
    ),
  );
  $newreleasequery= new WP_Query( $args );
  while ($newreleasequery->have_posts()) : $newreleasequery->the_post();
    $release_id=get_the_ID();
    get_album_version_count( $release_id );
    $count++;
  endwhile;
  echo 'All version counts have been reset. ', 'Count is ', $count;

There are too many posts in this query to run at once. How can I get it to piecemeal through the results and complete the function in chunks it can handle.

I’m guessing it has something to do with posts_per_page? Can I do something like this, and just keep writing more and more? It seems like there would be a faster/more efficient way to do this… Thanks all!

$args= array(
    'post_type' => 'release',
    'posts_per_page' => 250,
    'tax_query' => array(
        array(
            'taxonomy' => 'release-format',
            'field' => 'slug',
            'terms' => 'album',
        ),
    ),
  );
  $newreleasequery= new WP_Query( $args );
  $querycount= $newreleasequery->found_posts;
  //echo $querycount;
  
  while ($newreleasequery->have_posts()) : $newreleasequery->the_post();
    $release_id=get_the_ID();
    get_album_version_count( $release_id );
    
  endwhile;
  wp_reset_query();
  if($querycount >= 250){
    $args= array(
        'post_type' => 'release',
        'posts_per_page' => 250,
        'offset' => 250,
        'tax_query' => array(
            array(
                'taxonomy' => 'release-format',
                'field' => 'slug',
                'terms' => 'album',
            ),
        ),
    );
    $newreleasequery= new WP_Query( $args );
    $querycount= $newreleasequery->found_posts;
    //echo $querycount;
    
    while ($newreleasequery->have_posts()) : $newreleasequery->the_post();
        $release_id=get_the_ID();
        get_album_version_count( $release_id );
        
    endwhile;
    wp_reset_query();
  }if($querycount >= 500){
    $args= array(
        'post_type' => 'release',
        'posts_per_page' => 250,
        'offset' => 500,
        'tax_query' => array(
            array(
                'taxonomy' => 'release-format',
                'field' => 'slug',
                'terms' => 'album',
            ),
        ),
    );
    $newreleasequery= new WP_Query( $args );
    $querycount= $newreleasequery->found_posts;
    //echo $querycount;
    
    while ($newreleasequery->have_posts()) : $newreleasequery->the_post();
        $release_id=get_the_ID();
        get_album_version_count( $release_id );
        
    endwhile;
    wp_reset_query();
  }
  
  echo 'All version counts have been reset. ', 'Count is ', $querycount;

0
RCOTWUnite 2 months 2022-10-13T11:37:29-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse