This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Hook the filter before calling the query. Note that we are using an anonymous | |
// function here and saving a reference to the `$fn` variable for use further down. | |
add_filter( 'posts_where', $fn = function ( $where, WP_Query $wp_query ){ | |
global $wpdb; | |
// Add a clause that ensures we only get posts with an ID greater than 200. | |
$where .= " AND {$wpdb->posts}.ID > 200"; | |
return $where; | |
}, 10, 2 ); | |
// Make the query. | |
$query = new WP_Query( [ | |
'post_type' => 'post', | |
'posts_per_page' => 10, | |
] ); | |
// Remove the filter after calling the query so it doesn't affect any subsequent queries. | |
remove_filter( 'posts_where', $fn, 10 ); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Hook the filter before calling the query. Note that we are using an anonymous | |
// function here and saving a reference to the `$fn` variable for use further down. | |
add_filter( 'posts_clauses', $fn = function ( $pieces, WP_Query $wp_query ){ | |
global $wpdb; | |
// Add a clause that ensures we only get posts with an ID greater than 200. | |
$pieces['where'] .= " AND {$wpdb->posts}.ID > 200"; | |
return $pieces; | |
}, 10, 2 ); | |
// Make the query. | |
$query = new WP_Query( [ | |
'post_type' => 'post', | |
'posts_per_page' => 10, | |
] ); | |
// Remove the filter after calling the query so it doesn't affect any subsequent queries. | |
remove_filter( 'posts_clauses', $fn, 10 ); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'pre_user_query', $fn = function ( WP_User_Query $query ) { | |
global $wpdb; | |
$query->query_where .= " AND {$wpdb->users}.ID > 1"; | |
} ); | |
$query = new WP_User_Query( [ | |
'orderby' => 'ID', | |
'order' => 'ASC', | |
'fields' => 'ID', | |
'count_total' => false, | |
] ); | |
remove_action( 'pre_user_query', $fn ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment