DocumentationOnline documentation for Ajax Load More
pre_get_posts
In WordPress, pre_get_posts is a hook that allows for modification of an existing WP_Query before the query is run on the server.
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 pre_get_posts.
In the code sample below, an instance Ajax Load More with the ID of design-listing will be modified to return all posts of the design category.
// [ajax_load_more id="design-listing"]
function my_custom_category( $query ) {
if ( isset( $query->query['alm_id'] ) && $query->query['alm_id'] === 'design-listing' ) {
$query->set( 'category_name', 'design' );
$query->set( 'posts_per_page', -1 );
}
}
add_action( 'pre_get_posts', 'my_custom_category' );
PHPNote: The alm_id query parameter is a direct reference to the ID shortcode parameter – you must set a unique ID to target a specific Ajax Load More query.