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.
1 2 3 4 5 6 7 8 |
// [ajax_load_more id="design-listing"] add_action( 'pre_get_posts', 'my_custom_category' ); 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 ); } } |
Note: 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.