Wordpress main loop change impacts on server?

Asked

Viewed 32 times

1

I used pre_get_posts to change the loop of my Wordpress site. The idea of this loop is to not display posts that I set 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 this?

I feel that the homepage of my site is loading more slowly, already, for example, the page of categories 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;

}
});
  • Are you sure the problem is pre_get_posts? If I remember correctly, it is highly recommended to use it... already ruled out that it is a problem of Theme or some Plugin?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.