I want to override a 3rd party plugin action with a new action, but in comments to my another question @pieter-goosen suggested that this plugin action is very very expensive, in terms of using server resources. So, my question is how to improve this plugin action to make it less expensive?
The only thing that I think will improve this plugin action is to add a new query argument - 'fields' => 'ids'
, that will improve its performance. Are here other things to do for this?
add_action( 'adverts_event_expire_ads', 'adverts_event_expire_ads' );
/**
* Expires ads
*
* Function finds Adverts that already expired (value in _expiration_date
* meta field is lower then current timestamp) and changes their status to 'expired'.
*
* @since 0.1
* @return void
*/
function adverts_event_expire_ads() {
// find adverts with status 'publish' which exceeded expiration date
// (_expiration_date is a timestamp)
$posts = new WP_Query( array(
"fields" => "ids", //added for performance
"post_type" => "advert",
"post_status" => "publish",
"meta_query" => array(
array(
"key" => "_expiration_date",
"value" => current_time( 'timestamp' ),
"compare" => "<="
)
)
) );
if( $posts->post_count ) {
foreach($posts->posts as $post) {
// change post status to expired.
$update = wp_update_post( array(
"ID" => $post->ID,
"post_status" => "expired"
) );
} // endforeach
} // endif
}
UPDATE
I updated for performance (I hope) and overrode the code, so it now not just 'expire' posts, but also inform post authors about this, and it works (tested). But the question is the same: can it be improved/optimised for performance?
remove_action( 'adverts_event_expire_ads', 'adverts_event_expire_ads' );
add_action( 'adverts_event_expire_ads', 'my_adverts_event_expire_ads' );
/**
* Expires ads
*
* Function finds Adverts that already expired (value in _expiration_date
* meta field is lower then current timestamp) and changes their status to 'expired'.
*
* @since 0.1
* @return void
*/
function my_adverts_event_expire_ads() {
// Set our query arguments
$args = [
'fields' => 'ids',
'post_type' => 'advert',
'post_status' => 'publish',
'meta_query' => [
[
'key' => '_expiration_date',
'value' => current_time( 'timestamp' ),
'compare' => '<='
]
]
];
// get adverts with status 'publish' which exceeded expiration date
// (_expiration_date is a timestamp)
$posts = get_posts( $args );
// Check if we have adverts with exceeded expiration date, if not, return false
if ( !$posts )
return false;
// If we have posts with exceeded expiration time, lets change their status to expired
foreach($posts as $id) {
// change post status to expired.
$update = wp_update_post( array(
"ID" => $id,
"post_status" => "expired"
) );
// Send an email about expiration to the post author
$post = get_post($id);
$author = get_userdata($post->post_author);
$author_name = get_post_meta( $id, 'adverts_person', true );
$message =
"Hi " . $author_name . ", " .
"\n\nYour advert, \"" . $post->post_title . "\", expired and will be deleted in 7 days." .
"\n\nBest regards, \n\"" . get_bloginfo() . "\" team";
wp_mail($author->user_email, "Your article expired!", $message);
} // endforeach
}
"fields" => "ids"
to theWP_Query()
, but you are right, I don't tested it, because I don't overrided it yet. – Iurie Malai Nov 19 '15 at 20:50