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.

  • What are the conditions exactly? – Krzysiek Dróżdż Dec 30 '18 at 16:56
  • @KrzysiekDróżdż It is a WooCommerce site. This particular page only displays products that have atleast one another product in the same category with similar attributes. – A.Jesin Dec 30 '18 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.