pagination - Excluding posts without affecting the number of posts per page

admin2025-06-05  0

I'm in a situation where I have to exclude certain posts dynamically using a complex set of conditions*. I'm currently using the_posts filter to remove posts that don't meet the condition.

add_filter( 'the_posts', function( $posts, $query ) {
    foreach ( $posts as $i => $post ) {
        if ( check_custom_conditons( $post->ID ) ) {
            unset( $posts[ $i ] );
        }
    }
    return $posts;
}, 10, 2);

Problem is each page has an irregular number of posts as some of them were excluded after the query was run. Is it possible to maintain the same number of posts per page after excluding certain posts?


* The condition checks the parent category of the post for other posts with similar attributes and returns true if found and false if not found. I can't find a way to do this before the query is run.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749058014a315996.html

最新回复(0)