Skip to content

Instantly share code, notes, and snippets.

@wpfan
wpfan / gist:1549636
Created January 2, 2012 06:49
Order posts alphabetically using the posts_orderby hook
<?php
add_filter('posts_orderby', 'blog_change_sort', 10, 2);
function blog_change_sort( $orderby, $query ) {
// Only modify main query
// On the tutorial category page
if( $query->is_main_query() && is_category('tutorials') ) {
return "post_title ASC";
@wpfan
wpfan / gist:1549864
Created January 2, 2012 08:27
Order posts alphabetically using the pre_get_posts hook
<?php
add_action('pre_get_posts', 'blog_change_sort');
function blog_change_sort( $query ) {
// Only modify main query
// On the tutorial category page
if( $query->is_main_query() && is_category('tutorials') ) {
$query->set('orderby', 'title');
@wpfan
wpfan / gist:1553532
Created January 3, 2012 04:47
Display wordpress post queries using the 'posts_request' hook.
<?php
add_filter( 'posts_request', 'show_posts_request' );
function show_posts_request( $request ) {
echo $request;
return $request;
}
?>
@wpfan
wpfan / gist:1553924
Created January 3, 2012 07:25
Exclude category from blog using pre_get_posts hook
<?php
add_action( 'pre_get_posts', 'blog_exclude_cat' );
function blog_exclude_cat( $query ) {
// Only modify the main query
// On the home page
if( $query->is_main_query() && $query->is_home() ) {
$query->set( 'cat', '-8' );