Pagination Problem When Excluding Certain Categories from Blog Main Page

In our newest theme, I had to exlcude a category from main loop of that theme. The wordpress codex said that we could use query_post() function to do that

Then they gave an example,  if I wanted to exclude a category with ID=3, I could add this line to my index.php file, before main loop lines:

<?php
if (is_home()) {
query_posts("cat=-3");
}
?>

It worked! The posts from category I wanted to exclude didn’t show up

wordpress

But, unfortunately, the link to show previous post in my main page didn’t work. If I clicked the link, It didnt show posts that should appear in the second page. Instead, It kept showing posts from the front page

After googling for the answer, I found out that actually, this query_post() function override paged offset.

To get proper pagination,  we should recreate it through pagination parameter of query_post

First, we ask wordpress the page that we are on

To do this, we use get_query_var() function:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
?>

That line means if the ‘paged’ query variable is available, $page will receive value of that variable(wich is page number). If not, we assume we are on the first page, and give $page value 1.

and finally, we  use $paged as pagination parameter of query_post() :

<?php
query_post("cat=-3&paged=$paged");
?>

This line means…show posts of the page=$paged, except posts from category with ID=3

In short, if you want to exclude category with ID=3 (for example) from your blog main page, place this code before the main loop:

<?php
if (is_home()) {
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("cat=-3&paged=$paged");
}
?>

And category 3 will be exclude without any problem in pagination.

credit: Kafkaesqui at wordpress.org

33 Comments to “Pagination Problem When Excluding Certain Categories from Blog Main Page”

Add Comments (+)

  1. gjac says:

    THANK YOU SO MUCH!!!!!

  2. TSwain says:

    I don’t usually reply to posts but I will in this case, great info…I will add a backlink and bookmark your site. Keep up the good work!

  3. WP Themes says:

    Amiable dispatch and this fill someone in on helped me alot in my college assignement. Thanks you for your information.

  4. Steave says:

    Interesting post as for me. I’d like to read a bit more about that theme. Thnx for posting that info.

  5. WP Themes says:

    Nice post and this enter helped me alot in my college assignement. Gratefulness you on your information.

  6. Thanks a lot for this. I was experiencing this same problem since yesterday when I wanted to exclude a category from the posts page of a client site that im working on and this solution worked great! Subscribing now.

  7. stratosferik says:

    Thank you for the code, this saved me a lot of time trying to figure out how to solve this.

  8. admin says:

    Your welcome

    The problem wont take lot of your time if you use the right keyword to google the solutions :D

    nice site you have there stratosferik :)

  9. WP Themes says:

    Good brief and this enter helped me alot in my college assignement. Thank you on your information.

  10. Shane says:

    Thanks so much man. I was being driven demented looking for a solution to this problem. Glad you could help me out!

  11. kristina says:

    the syntax for this was off for me, however i found a fix. if anyone has the same problem, try this:

    note: i changed it from “if on the homepage” to “if posts exist”

  12. Jeremia says:

    Thanks a ton for this, I was running into the same problem when trying to set up my categories. Spent a good deal of time trying to figure this out – and your solution worked perfectly :-)

  13. David White says:

    I looked all over the WordPress codex for answers to this, and after lots of code changes, found nothing that solved the problem.

    This correctly written code solved the problem straight away. Thanks for publishing this – it’s saved me a real headache – I was ready to rebuild the website in a new template!

    Thanks again

    David

  14. Duane says:

    Saved me a lot of googlingthank you so much. cheers.

  15. J says:

    What about if i want to exclude multiple categories?

  16. vCopia says:

    Boy Kafkaesqui, that string is exactly what I was looking for. I, too, used

    <?php
    if (is_home()) {
    query_posts(“cat=-3″);
    }
    ?>

    and only today realized it didn’t exclude posts past page 1. You’re code work perfectly, down to the category #. :) Genuinely appreciate your sharing it. Saved me hours of research and testing.

    Thanks
    vCopia

  17. afarras says:

    I have two languages in my blog, and when i try to go to older posts of one category, wordpress change to default language and not the language that i’m using in the moment
    any solutions

    thnaks

  18. Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your website? My website is in the exact same niche as yours and my visitors would definitely benefit from a lot of the information you present here. Please let me know if this okay with you. Cheers!

  19. Rebekah says:

    Yes! This fixed that issue perfectly! Thanks :)

  20. edu says:

    I love you!!!

    thank you!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! :)

  21. Melinka says:

    Thanks a lot!!! Finally!!

  22. Jorch says:

    thanks a lot bud!! you saved my life

  23. DJ says:

    I’m using the following code (as per your suggestion) on my homepage. The homepage lists the 5 most recent post …. but the OLDER POSTS link takes me to a page that lists the same 5 most recent posts (not 6-10)

    $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
    echo $paged;
    query_posts(‘posts_per_page=’.$numposts.’order=DESC&paged=’.$paged);

    So, oddly enough, I changed paged=’.$paged to paged=’.$page
    $page outputted the page you were on. $paged always displayed ’1′ for some reason. So, here is what worked for me…

    $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
    query_posts(‘posts_per_page=’.$numposts.’order=DESC&paged=’.$page);

  24. vietpn says:

    like this @vietpn …

  25. Joseph01 says:

    Thank You Maaaaaaaaaaaaan You Just solved my problem ;)

  26. Jason says:

    Thanks very much. This post offered a solution that I was unable to find on the WordPress forum.

    In case anyone is wondering, the code also works if you need to limit results to one category, rather than exclude posts from one category — i.e. it also works for cat=X, not just cat=-X.

  27. Dave says:

    When you made these changes, what pages did you make the changes to? Your post never says or indicates which php file(s) you had to modify.

    Thanks!

  28. Roland says:

    Cheers mate…

Trackbacks/Pingbacks

  1. Tweets that mention Pagination Problem When Excluding Certain Categories from Blog Main Page | DynamicWP -- Topsy.com
  2. Problema con paginación cuando se excluyen Categorías en Wordpress

Leave a Reply

 

Amazingly Beautiful WordPress Themes