Why does next_posts(); function does not work inside header.php whereas it ^does work^ inside temlate-homepage.php ? My code below:


// Query ALL posts ordered by random

$args = array(

'post_type' => array('projekt'), // Posts

'orderby' => 'rand', // Order random

'posts_per_page' => 8, // get all posts

'ignore_sticky_posts' => true, // Do not preserve order of stickies


$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$the_query = new WP_Query( $args );

echo next_posts();


....even when I add a parameter like 'max_num_pages' to next_posts(); function -> it does not work in ^header.php^

...it seems like get_header(); command has something to do with it, because when I paste the whole header.php code inside template-homepage.php then next_posts() works properly which means if I'm on /page/3/ -> next_posts() will return /page/4/ url...

  • What is $the_query for? Also, it looks like you're trying to paginate random results, which doesn't make sense, since each page is going to be random, and won't have anything to do with what was on the previous page.. – Jacob Peattie Feb 8 at 10:55
  • @JacobPeattie there is nothing wrong with paginating the result of a query with a random orderby parameter. There is always a current, next and previous result – Yashar Feb 8 at 15:04
  • @Yashar. There's pages, but they don't mean anything. The randomness isn't persistent, so a post on page 1 could end up appearing on all pages, or a post might not appear on any pages. – Jacob Peattie Feb 8 at 15:08
  • @JacobPeattie Link1 Link2 Link3 – Yashar Feb 8 at 15:39
  • @Piotr Welcome to stackexchange. What do you mean by 'not working'? Any error or warnings on debug.log or any unpredicted result? next_posts() is defined in link-template.php and it gets included after general-template.php file, which includes get_header() function! But it should not affect using Plugin API, try using get_header action hook. – Yashar Feb 8 at 15:52

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.