Thursday, October 13, 2022
HomeWordPress Developmentloop - How do I cut up a big question with a...

loop – How do I cut up a big question with a semi-expensive operate included into a number of smaller queries


I’ve a depend saved in post-meta referred to as version-count. I simply wrote a bit plugin to delete the information saved on this post-meta utilizing delete_post_meta_by_key.

I would like my plugin to then undergo every submit and re-run my count-versions operate, which saves the brand new depend in post-meta.

The issue is that after I run this code, I get a 504 error. My question returns 8,000+ outcomes.

$args= array(
    'post_type' => 'launch',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'release-format',
            'subject' => 'slug',
            'phrases' => 'album',
        ),
    ),
  );
  $newreleasequery= new WP_Query( $args );
  whereas ($newreleasequery->have_posts()) : $newreleasequery->the_post();
    $release_id=get_the_ID();
    get_album_version_count( $release_id );
    $depend++;
  endwhile;
  echo 'All model counts have been reset. ', 'Depend is ', $depend;

There are too many posts on this question to run without delay. How can I get it to piecemeal via the outcomes and full the operate in chunks it will probably deal with.

I am guessing it has one thing to do with posts_per_page? Can I do one thing like this, and simply hold writing increasingly more? It looks as if there could be a sooner/extra environment friendly means to do that… Thanks all!

$args= array(
    'post_type' => 'launch',
    'posts_per_page' => 250,
    'tax_query' => array(
        array(
            'taxonomy' => 'release-format',
            'subject' => 'slug',
            'phrases' => 'album',
        ),
    ),
  );
  $newreleasequery= new WP_Query( $args );
  $querycount= $newreleasequery->found_posts;
  //echo $querycount;
  
  whereas ($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' => 'launch',
        'posts_per_page' => 250,
        'offset' => 250,
        'tax_query' => array(
            array(
                'taxonomy' => 'release-format',
                'subject' => 'slug',
                'phrases' => 'album',
            ),
        ),
    );
    $newreleasequery= new WP_Query( $args );
    $querycount= $newreleasequery->found_posts;
    //echo $querycount;
    
    whereas ($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' => 'launch',
        'posts_per_page' => 250,
        'offset' => 500,
        'tax_query' => array(
            array(
                'taxonomy' => 'release-format',
                'subject' => 'slug',
                'phrases' => 'album',
            ),
        ),
    );
    $newreleasequery= new WP_Query( $args );
    $querycount= $newreleasequery->found_posts;
    //echo $querycount;
    
    whereas ($newreleasequery->have_posts()) : $newreleasequery->the_post();
        $release_id=get_the_ID();
        get_album_version_count( $release_id );
        
    endwhile;
    wp_reset_query();
  }
  
  echo 'All model counts have been reset. ', 'Depend is ', $querycount;

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments