I need some posts excludes from displaying in wp_query in some conditons. I use below code to do it, and the code works correctly, but I set the posts_per_page to 12 and by this code the skiped posts are counted , for example instead of 12 post in each page it has different post numbers (10,2,5 , ...)

while( $query->have_posts() ){ 


            //Show the post

  • 1
    What is $course_options? Is level a meta_key?
    – kero
    Jan 28, 2018 at 14:21
  • see the updated question Jan 28, 2018 at 14:38

2 Answers 2


To have the pagination work properly you need to filter the posts at the WordPress main query level using the pre_get_posts action.

By looking at your code, I don't see where the post specific information is involved. Is $course_options holding information for the post?


It will be tricky to make the pagination work when you are using php to filter out posts. When you go to page 2 for example the query will have an offset and limit parameter, effectively grabbing posts from 12 to 24 directly from the sql. filtering those won't do you any good unless the sql knows about which of the first 12 it has to skip. In most cases you are going to want to look into using a meta_query in the $wp_query, or your get_posts() function (both rely use more or less the same code in the end). A meta query will work for many things but if your condition is too complicated then it just might be the case that you cannot replicate the filtering via sql. please let us know the condition and i can show you how to write in a meta query. also if you did not write the $wp_query yourself (ie. your on a page where it was already run for you like archive.php) then you'll need to use the pre_get_posts action to add the meta_query to the $wp_query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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