ControlledChaos
12/28/2016 - 10:00 PM

Merge an array of query variables with an existing WordPress query.

Merge an array of query variables with an existing WordPress query.

Merge Query Variables

WordPress Snippet

I was tasked with alphabetizing the archive of a custom post type using an off-the-shelf theme. The theme's archive.php file used the global variable global $wp_post to set the query.

The example here was placed in a custom post type archive (archive-example.php) following the global variable to merge with the default query. One can use this with any of the query variables.

<?php

// Default query
global $wp_post;

// Merge with default query
$args = array_merge( $wp_query->query_vars, array( 'orderby' => 'title', 'order' => 'ASC' ) );
query_posts( $args );

?>