Skip to content

Instantly share code, notes, and snippets.

@mishterk
Last active May 26, 2022 00:06
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 mishterk/04a20b60addddb3878db17531d40a6fe to your computer and use it in GitHub Desktop.
Save mishterk/04a20b60addddb3878db17531d40a6fe to your computer and use it in GitHub Desktop.
<?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 );
<?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 );
<?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