I used pre_get_posts
to change the loop of my Wordpress site. The idea of this loop is to not post posts that I define an expiration date, whose dates are added in a custom field
( postexpiry
).
Do these key queries (present in my code) require more server work? Does anyone have a better suggestion than that?
I feel like the homepage of my site is loading slower, for example, the category page does not.
add_action( 'pre_get_posts', function( $q ) {
if ( (is_home() || is_category() || is_tag() || is_search()) && $q->is_main_query() && !is_admin() )
{
$q->set( 'post_type', array( 'post', 'news' ) );
$date_now = strtotime(current_time( 'mysql' ));
$meta_query = array(
'relation' => 'OR',
array(
'key' => 'postexpiry',
'compare' => 'NOT EXISTS',
),
array(
'key' => 'postexpiry',
'value' => '0',
'compare' => '=',
'type' => 'NUMERIC',
),
array(
'relation' => 'AND',
array(
'key' => 'postexpiry',
'value' => $date_now,
'compare' => '>',
'type' => 'NUMERIC',
),
),
);
$q->set( 'meta_query', $meta_query );
return $q;
}
});