reset have_posts after unsetting a post
-
Hi there,
We have a specific event that is scheduled that grabs an alert post and gets certain jobs.
This then runs through a function that removes posts from these if they have been sent out to a specific user.This works fine by unsetting the post in the object according to the index. However after this and sorting the posts, when the function have_posts() is called this results in NULL returned.
Can someone please help ecxplain why this would then be null if there are still posts within the query object?
Thanks
Andi
Here is the code…
protected function filter_jobs_for_send_once_only($jobs, $user, $alert){ $i = 0; $user_id = $user->ID; $alert_id = $alert->ID; $original_post_count = $jobs->post_count; error_log ("\n\nPRE alert count jobs " . $jobs->post_count . " != " . $original_post_count . " HAVE POSTS: " . $jobs->have_posts() . "\n\r" . print_r($jobs,true) , 3 , '/var/www/vhosts/websitetest.com/httpdocs/websitetest/testCountJobs.log', NULL ); while ( $jobs->have_posts() ) { $jobs->the_post(); $post_id = get_the_ID(); $meta_send_check = get_post_meta( $post_id, 'send_once_only', true); $meta_send_date_check = get_post_meta( $post_id, 'last_date_sent', true); $meta_user_data_check = get_post_meta( $post_id, 'user_sent_list'); $dateNow = new DateTime('now'); /* Multidimensional array * */ $user_list = (empty($meta_user_data_check[0]) ? array() : $meta_user_data_check[0]); $user_alert_list = (empty($user_list[$user_id]) ? array() : $user_list[$user_id]); switch($meta_send_check) { case true: /** Case true: Send once only is set to true we then want to see if it has a last_date_sent meta data set. * If it has then we unset it from the jobs array, if not we do not unset it but add a last_date_sent * so that it can be filtered out next time round, stopping the list from growing * * However we now ignore it if this job has not yet been sent to a new user * */ if(in_array($user_id, array_keys($user_list)) && in_array($alert_id, $user_alert_list) ) { /// Unset as has been sent already in the past and we don't want to again unset($jobs->posts[$i]); $jobs->found_posts--; $jobs->post_count--; } break; case false: /** keep in list, but Set a last_date_sent, so if an admin sets the job to only_send_once = true then the case above will be respected * for this job */ break; } /// End Switch ///test array increases if(!in_array($alert_id, $user_alert_list)){ array_push($user_alert_list, $alert_id); $user_list[$user_id] = $user_alert_list; } /// Sort the array ksort($user_list); update_post_meta($post_id, 'last_date_sent', $dateNow->format('Y-m-d H:i:s')); update_post_meta($post_id, 'user_sent_list', $user_list); $i++; } /// End while loop if($jobs->post_count != $original_post_count) { ///sort($jobs->posts); $jobs->posts = array_values($jobs->posts); ///// No have_post... can't work out why error_log ("\n\nalert count jobs " . $jobs->post_count . " != " . $original_post_count . " HAVE POSTS: " . $jobs->have_posts() . "\n\r" . print_r($jobs,true) , 3 , '/var/www/vhosts/websitetest.com/httpdocs/websitetest/testCountJobs.log', NULL ); } return $jobs; }
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘reset have_posts after unsetting a post’ is closed to new replies.