Skip to content

Instantly share code, notes, and snippets.

@wpsmith
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save wpsmith/11144585 to your computer and use it in GitHub Desktop.

Select an option

Save wpsmith/11144585 to your computer and use it in GitHub Desktop.
PHP: Use page_template within pre_get_posts
<?php
add_filter( 'pre_get_posts', 'wps_archive_query' );
/**
* Add more query args based on selected page template.
*
* @since 1.0.0
*
* @param <type> $query
*
* @return <type>
*/
function wps_archive_query( $query ) {
if ( is_page() ) {
if ( 'page-stats.php' == get_page_template_slug( $query->queried_object_id ) ) {
$query->set( 'post_date', date( 'Y-m-d', strtotime( '-30 days' ) ) );
$query->set( 'post_type', 'my_cpt' );
$query->set( 'orderby', 'comment_count' );
$query->set( 'order', 'ASC' );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment