Skip to content

Instantly share code, notes, and snippets.

@musen
Created February 2, 2015 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save musen/00ba40aa5edf4fc151c2 to your computer and use it in GitHub Desktop.
Save musen/00ba40aa5edf4fc151c2 to your computer and use it in GitHub Desktop.
Wordpress pre_get_posts action hook. Comes in handy when you would like to modify query variables before running archive queries.
//order products alphabetically and display all of them
function prefix_products_archive_modify( $query ) {
if ( is_post_type_archive('product') &&
! empty( $query->query['post_type']) &&
$query->query['post_type'] == 'product') {
$query->set( 'orderby', 'title' );
$query->set('order', 'ASC');
$query->set( 'posts_per_page', -1 );
}
return $query;
}
add_action( 'pre_get_posts', 'prefix_products_archive_modify' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment