Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.