Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

migrated from stackoverflow.com Apr 28 '12 at 5:03

This question came from our site for professional and enthusiast programmers.

2 Answers

up vote 2 down vote accepted

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!

share|improve this answer
    
I didn't know if there was a way, should I place this right into the category loop? (I am a noob) –  snooker841 Apr 28 '12 at 6:00
    
Nope... put it in the functions.php file... –  Rutwick Gangurde Apr 28 '12 at 6:09
    
It works perfect, just one thing though it is conflicting with "list category posts" plugin which also displays content with an offset. it seems to altering their offsets also. is there a way to keep it from altering their outputs? –  snooker841 Apr 28 '12 at 6:14
    
Do you need that plugin at all? You could set the priority of my action in a way that it runs after or before that plugin. –  Rutwick Gangurde Apr 28 '12 at 6:27
    
Btw, I've edited my code to include a very important check, please note it. –  Rutwick Gangurde Apr 28 '12 at 6:31
show 15 more comments

Don't forget to check $query->is_main_query()Codex:

<?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( $query->is_main_query() && !is_admin() && $query->is_category() ) {
        $query->set( 'offset', 3 );
    }
}
?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.