An archive page is where we list category, tag, date, and author, etc.. posts.

If you want to modify archive Query what you will do ?. Most of the developer write own WP_Query in Archive Page. Actually this way we double up the cost of getting the result. First the WordPress default Query and second own Query.

The best way to modify the archive query use the pre_get_posts  filter instead of writing their own Query. The unique thing of this filter you don’t need to return any value use set() method to modify or add a new parameter in the query.

Let’s see the example below ?

/**
 * Modifing Archive Query.
 *
 * @author https://twitter.com/sharazghouri1
 *
 * @param object $query
 * @return void
 */
function as_archive_per_page( $query ) {

	if ( $query->is_main_query() && ( is_tax( 'product_cat' )  ) ) {
      
		// Change order.
		$query->set( 'order', 'ASC' );
      
        // Change posts per page for paginaiton.
      	$query->set( 'posts_per_page', 20 );
		
      	// Add meta query.
		$meta_query = [ 
        	[
			 'relation' => 'AND',
			],
        	[
				'key'     => 'template_type',
				'value'   => 'apps',
				'compare' => '=',
			]
          ];
            
			$query->set( 'meta_query', $_meta_query );
	}

}
add_filter( 'pre_get_posts', 'as_archive_per_page', 10 );

Did you notice we are not returning any value ??

[sibwp_form id=3]
There are currently no comments.