Skip to content

Instantly share code, notes, and snippets.

@wpsmith
Last active December 10, 2015 20:38
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 wpsmith/4489534 to your computer and use it in GitHub Desktop.
Save wpsmith/4489534 to your computer and use it in GitHub Desktop.
Customizing the WordPress Query
<?php
add_action( 'pre_get_posts', 'wps_change_posts_per_page_in_grid' );
/**
* Change Posts Per Page
*
* @author Bill Erickson
* @author Travis Smith
* @link http://www.billerickson.net/customize-the-wordpress-query/
* @link http://wpsmith.net/2013/genesis/genesis-grid-loop-in-genesis-1-9/
* @param object $query WP Query data
*
*/
function wps_change_posts_per_page_in_grid( $query ) {
global $wp_the_query;
if( $query->is_main_query() && wps_is_doing_grid_loop() ) {
$query->set( 'posts_per_page', '15' );
}
}
<?php
add_action( 'pre_get_posts', 'wps_exclude_cat_in_grid' );
/**
* Exclude Category from Grid
*
* @author Bill Erickson
* @author Travis Smith
* @link http://www.billerickson.net/customize-the-wordpress-query/
* @link http://wpsmith.net/2013/genesis/genesis-grid-loop-in-genesis-1-9/
* @param object $query WP Query data
*
*/
function wps_exclude_cat_in_grid( $query ) {
if( $query->is_main_query() && wps_is_doing_grid_loop() ) {
$query->set( 'cat', '-1,-2' );
}
}
<?php
add_action( 'pre_get_posts', 'wps_include_cat_in_grid' );
/**
* Limit Query to one Category
*
* @author Bill Erickson
* @author Travis Smith
* @link http://www.billerickson.net/customize-the-wordpress-query/
* @link http://wpsmith.net/2013/genesis/genesis-grid-loop-in-genesis-1-9/
* @param object $query WP Query data
*
*/
function wps_include_cat_in_grid( $query ) {
if( $query->is_main_query() && wps_is_doing_grid_loop() ) {
$query->set( 'cat', '4' );
}
}
<?php
/**
* Possibly amend the loop.
*
* Specify the conditions under which the grid loop should be used.
*
* @author Bill Erickson
* @author Gary Jones
* @author Travis Smith
* @link http://code.garyjones.co.uk/genesis-grid-loop-advanced/
* @link http://wpsmith.net/2013/genesis/genesis-grid-loop-in-genesis-1-9/
*
* @return boolean Return true of doing the grid loop, false if not.
*/
function wps_is_doing_grid_loop() {
// Amend this conditional to pick where this grid looping occurs.
// This says to use the grid loop everywhere except single posts,
// single pages and single attachments.
return ( ! is_singular() );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment