Skip to content

Instantly share code, notes, and snippets.

@Page-Carbajal
Last active August 29, 2015 14:21
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 Page-Carbajal/cd8fba16f7ab09a31c27 to your computer and use it in GitHub Desktop.
Save Page-Carbajal/cd8fba16f7ab09a31c27 to your computer and use it in GitHub Desktop.
WordPress pre_get_posts Filter
<?php
namespace WordPress\Filters;
class PreGetPosts{
public $filterPriority = 10;
public function __construct( $priority = 10 ){
//TODO: Set your custom properties if needed
$this->filterPriority;
}
public function init(){
add_filter( 'pre_get_posts', array( __CLASS__, 'preGetPostsFilter' ), $this->filterPriority );
}
/***
* Description: This filter modifies the tax_query var to load only quotes and status post formats
* @param $wpQuery: a pointer passed to the global variable $wp_query;
*/
public static function preGetPostsFilter( $wpQuery ){
if( $wpQuery->is_main_query() && is_home() ){
$wpQuery->set( 'tax_query', array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-link', 'post-format-status' ),
'operator' => 'IN'
),
# Add more filter for the taxonomy. Like this one, which filters out all images
#array(
# 'taxonomy' => 'post_format',
# 'field' => 'slug',
# 'terms' => array( 'post-format-image' ),
# 'operator' => 'NOT IN'
#)
)
);
}
}
}
//To make this work just write a require('PATHTOFILE') command in your functions.php
$wpFilters = new \WordPress\Filters\PreGetPosts( 1 );
//
add_action( 'init', array( $wpFilters, 'init' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment