posts_where
In WordPress, posts_where is a hook used to modify the WHERE clause of an SQL query underlying a WP_Query. With Ajax Load More, there are a couple of ways you can use a posts_where filter to modify the custom plugin query.
Custom Args
Use the custom_args parameter to pass custom query options directly to the Ajax Load More WP_Query.
In the code sample below, a starts_with key/value pair has been set via Ajax Load More shortcode using the custom_args parameter and then accessed in the posts_where filter to modify the SQL query on the server side.
// [ajax_load_more custom_args="starts_with:a"]
function alm_first_letter_query($where, $query) {
$starts_with = $query->get( 'starts_with' );
if ( $starts_with ) {
global $wpdb;
$where .= " AND $wpdb->posts.post_title LIKE '$starts_with%'";
}
return $where;
}
add_filter( 'posts_where', 'alm_first_letter_query', 10, 2 );
PHPQuery ID
All Ajax Load More queries have a custom alm_id parameter attached to the WP_Query, this parameter allows developers to target a specific instance of Ajax Load More for modification while using posts_where.
The following code sample will run the posts_where filter only when this specific instance of Ajax Load More is active.
// [ajax_load_more id="my-query"]
function alm_custom_posts_query($where, $query) {
$alm_id = $query->get( 'alm_id' );
if ( $alm_id === 'my-query' ) {
global $wpdb;
$where .= " AND LENGTH({$wpdb->prefix}posts.post_content) > 1200";
}
return $where;
}
add_filter( 'posts_where', 'alm_custom_posts_query', 10, 2 );
PHP