I have been on this for a while now and haven’t really come across a solution to filter the posts on a category page using a sort of offset in php.

There are plugin for posts and pages but I am trying to achieve this with pure php and specifically for a category page.

Scenario example:
A blog with 7 posts, setting the offset to say 3 would begin the loop on the category page to only show (Post4, Post5, Post6, Post7) and NOT (Post1, Post2, Post3).

I am wondering if this would be possible at all and be dynamic enough to apply to all categories without the need to specifically identify which posts to exclude via id numbers.

2 Answers
2

The ‘offset’ parameter does what you want. I’ve written this hack, it should help you…

<?php
    //The third parameter corresponds to action priority,
    //set it to change the order of execution in case of a conflict
    add_action('pre_get_posts', 'the_modified_loop', 10);

    function the_modified_loop($query){
        //Remove 'is_admin' if you want the code to run for the backend category archive page
        if(!is_admin() && $query->is_category()){
            $query->set('offset', 3);
        }
    }
?>

Let me know if it worked!

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *