1- Fetch only ids in WP_Query

'fields' => 'ids',

2- If it’s non paging query use no_found_rows true.

'no_found_rows' => true,

3- ignore_sticky_posts is used to exclude sticky post from your custom query.

'ignore_sticky_posts' => true,

4- Use date_query parameter in WP_Query

$start_date = gmdate( 'Y-m-d', strtotime( '-6 months' ) );
$end_date   = gmdate( 'Y-m-d' );
$args['date_query'] = array(
			array(
			     'after'     => $start_date,
			     'before'    => $end_date,
			     'inclusive' => true,
			),
		);

5- Do not use post__not_in. Avoid post__not_in

6- Use object cache to store query output. save only query not entire html.

Final query should be like below query.

$start_date = gmdate( 'Y-m-d', strtotime( '-6 months' ) );
$end_date   = gmdate( 'Y-m-d' );
$args       = array(
			'post_type'           => 'post',
			'post_status'         => 'publish',
			'fields'              => 'ids',
			'suppress_filters'    => false,
			'ignore_sticky_posts' => true,
			'posts_per_page'      => 10,
			'no_found_rows'       => true,
                         'date_query'          => array(
			                            array(
			     'after'     => $start_date,
			     'before'    => $end_date,
			     'inclusive' => true,
			),
		); 
		);

$posts = new \WP_Query( $args );

Leave a comment